Profiling Python
Profiling means having the application run while keeping track of several different parameters, such as the number of times a function is called, and the amount of time spent inside it.
Profiling is closely related to debugging. Although the tools and processes used are quite different, both activities involve probing and analyzing your code to understand where the root of a problem lies, and then making changes to fix it. The difference is that instead of incorrect output or crashing, the problem we are trying to solve is poor performance.
Sometimes, profiling will point to where the performance bottleneck is, at which point you will need to use the debugging techniques we discussed earlier in this chapter to understand why a particular piece of code does not perform as well as it should. For example, faulty logic in a database query might result in loading thousands of rows from a table instead of just hundreds. Profiling might show you that a particular...