Oddly enough, what you see is the expected behavior. In the section on familiarizing with profile sections in Python docs, it says that profile adds “significant overhead to profiled programs” compared to cProfile . The difference you see is in the libraries you use, not about how you name them. Consider this script:
import profile import cProfile def nothing(): return def main(): for i in xrange(1000): for j in xrange(1000): nothing() return cProfile.run('main()') profile.run('main()')
The conclusion from cProfile shows that the main lesson takes about 0.14 seconds, and the profile option reports 1,645 seconds, which is 11.5 times more.
Now change the script again to the following:
def nothing(): return def main(): for i in xrange(1000): for j in xrange(1000): nothing() return if __name__ == "__main__": main()
And call it profilers:
Python profile -m test_script.py
Sends 1,662 seconds to launch the main page.
python -m cProfile test_script.py
Sends 0.143 seconds to start the main one.
This shows that the way you start the profilers has nothing to do with the mismatch you saw between cProfile and profile . The difference is due to the way the two profilers handle "events", such as function calls or returns. In both cases, your executable code has software interceptors that trigger callbacks to track these events and perform actions such as update counters for events and start or stop timers. However, the profile module handles all these events initially in Python, which means that your interpreter must leave your code, execute the callback material, and then return to continue working with your code.
The same thing happens with cProfile (perform profiling callbacks), but it is much faster because the callbacks are written in C. Look at the two module.py and cProfile.py module files showing some differences:
- profile.py - 610 lines, and cProfile.py - only 199 - most of its functions are processed in C.
- profile.py primarily uses the Python libraries, and cProfile.py imports the _lsprof file, the C code file. The source can be viewed here .
- The
profile class in profile.py does not inherit from any other class (line 111), and the profile class in cProfile.py (line 66) inherits from _lsprof.Profiler , which is implemented in the C source file.
As a rule, cProfile is usually the way to go, simply because it is mainly implemented in C, so everything is faster.
As an aside, you can improve profile performance by calibrating it. Details on how to do this are available in the docs . More information on how / why it all happens the way it is done in the Python documentation sections on Deterministic profiling and limitations .
TL DR
cProfile much faster because, as its name implies, most of it is implemented in C. This is different from the profile module, which should handle all profiling callbacks in native Python. If you call profilers from the command line or manually inside your script does not affect the time difference between the two modules.