How to time how long a Python program takes to run? -
is there simple way time python program's execution?
clarification: entire programs
use timeit:
this module provides simple way time small bits of python code. has both command line callable interfaces. avoids number of common traps measuring execution times.
you'll need python statement in string; if have main function in code, use this:
>>> timeit import timer >>> timer = timer('main()', 'from yourmodule import main') >>> print timer.timeit() the second string provides setup, environment first statement timed in. second part not being timed, , intended setting stage were. first string run through it's paces; default million times, accurate timings.
if need more detail things slow, use 1 of python profilers:
a profiler program describes run time performance of program, providing variety of statistics.
the easiest way run using cprofile module command line:
$ python -m cprofile yourprogram.py
Comments
Post a Comment