Macro-profiling is done by running the application in a special mode, where the interpreter is instrumented to collect statistics on the code usage. Python provides several tools for this, including the following:
- profile: This is a pure Python implementation
- cProfile: This is a C implementation that provides the same interface as that of the profile tool, but has less overhead
The recommended choice for most Python programmers is cProfile due to its reduced overhead. Anyway, if you need to extend the profiler in some way, then profile will be a better choice because it doesn't use C extensions and so is easier to extend.
Both tools have the same interface and usage, so we will use only one of them here. The following is ...