Profiling a Python Script
Here are some simple ways to profile Python scripts.I heavily use this to check my Project Euler solutions.
SOLUTION 1:
The main common option would be to use the profile (or cprofile) module.There are two different ways of using it :
- As a module, by directly running
python -m cProfile script.py
- In your code, by importing the utilities
import cProfile
cProfile.run('function()') # in your __main__
SOLUTION 2:
If you want to avoid using a command line, or you don't have the profile module installed; here os another possibility. There is also the timeit module available.import timeit
t1 = timeit.Timer("function()", "from __main__ import function")
print t1.timeit(1)