5

Is there a simple way to time a Python program's execution?

clarification: Entire programs

5 Answers 5

8

Use timeit:

This module provides a simple way to time small bits of Python code. It has both command line as well as callable interfaces. It avoids a number of common traps for measuring execution times.

You'll need a python statement in a string; if you have a main function in your code, you could use it like this:

>>> from timeit import Timer
>>> timer = Timer('main()', 'from yourmodule import main')
>>> print timer.timeit()

The second string provides the setup, the environment for the first statement to be timed in. The second part is not being timed, and is intended for setting the stage as it were. The first string is then run through it's paces; by default a million times, to get accurate timings.

If you need more detail as to where things are slow, use one of the python profilers:

A profiler is a program that describes the run time performance of a program, providing a variety of statistics.

The easiest way to run this is by using the cProfile module from the command line:

$ python -m cProfile yourprogram.py
12
  • 1
    I looked at the link and found it a little confusing. Is there an easy script that shows how to use this? Jun 4, 2012 at 14:59
  • 1
    timeit is for micro-benchmarks: tiny program snippets, not whole programs.
    – user395760
    Jun 4, 2012 at 15:01
  • If all you want to know if it is fast or slow, timeit is fine. If you need to know where it is slow in a larger program, you need to profile.
    – Martijn Pieters
    Jun 4, 2012 at 15:09
  • @MartijnPieters I do not understand the arguments of Timer. Let's say I am putting my main program, def myProg(), in the script. At the very bottom, outside of the myProg() function, what would I put in the timer arguments? Jun 4, 2012 at 15:11
  • Timer('myProg()', 'from __main__ import myProg') should do it.
    – Martijn Pieters
    Jun 4, 2012 at 15:13
5

You might want to use built-in profiler.

Also you might want to measure function's running time by using following simple decorator:

import time
def myprof(func):
    def wrapping_fun(*args):
        start = time.clock()
        result = func(*args)
        end = time.clock()
        print 'Run time of %s is %4.2fs' % (func.__name__, (end - start))
        return result
    return wrapping_fun

Usage:

@myprof
def myfun():
    # function body 
3
  • That's a pretty poor profiling function. I wouldn't rely on it for anything worth benchmarking/profiling in the first place.
    – user395760
    Jun 4, 2012 at 15:02
  • 1
    Numerous reasons, most of which reduce to "none of us is smart enough to do this well off-hand". For starters, time.clock is not the most precise clock on all platforms (cf. default_clock() defined by timetit), and the output is very limited (intermingled with program output, need to collect and parse by hand). Minor issues include not accounting for exceptions and not using functools.wrap.
    – user395760
    Jun 4, 2012 at 15:08
  • @delnan: You're absolutely right, it's definitely not universal tool for profiling, but sometimes I use it as a snippet.
    – cval
    Jun 4, 2012 at 15:17
5

If you're on Linux/Unix/POSIX-combatible platform just use time. This way you won't interfere with you script and won't slow it down with unnecessarily detailed (for you) profiling. Naturally, you can use it for pretty much anything, not just Python scripts.

2

For snippets use the timeit module.

For entire programs use the cProfile module.

2

Use timeit

>>> import timeit  
>>> t = timeit.Timer(stmt="lst = ['c'] * 100")  
>>> print t.timeit()  
1.10580182076  
>>> t = timeit.Timer(stmt="lst = ['c' for x in xrange(100)]")  
>>> print t.timeit()  
7.66900897026  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.