cProfile – finding the slowest components

The profile module makes it easily possible to analyze the relative CPU cycles used in a script/application. Be very careful not to compare these with the results from the timeit module. While the timeit module tries as best as possible to give an accurate benchmark of the absolute amount of time it takes to execute a code snippet, the profile module is only useful for relative results. The reason is that the profiling code itself incurs such a slowdown that the results are not comparable with non-profiled code. There is a way to make it a bit more accurate however, but more about that later.


Within this section we will be talking about the profile module but in the examples we will actually use the ...

Get Python: Journey from Novice to Expert now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.