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.
Note
Within this section we will be talking about the profile
module but in the examples we will actually use the cProfile
module. The cProfile
module is a high-performance emulation of the pure Python profile
module.
Let's profile our Fibonacci function from Chapter 5, Decorators– Enabling Code Reuse by Decorating, both with and without the...