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. See also Tim Peters' introduction to the ``Algorithms'' chapter in the Python Cookbook, published by O'Reilly.
The module defines the following public class:
[stmt='pass'
[, setup='pass'
[, timer=<timer function>]]]) |
The constructor takes a statement to be timed, an additional statement
used for setup, and a timer function. Both statements default to
'pass'
; the timer function is platform-dependent (see the
module doc string). The statements may contain newlines, as long as
they don't contain multi-line string literals.
To measure the execution time of the first statement, use the timeit() method. The repeat() method is a convenience to call timeit() multiple times and return a list of results.
[file=None]) |
Typical use:
t = Timer(...) # outside the try/except try: t.timeit(...) # or t.repeat(...) except: t.print_exc()
The advantage over the standard traceback is that source lines in the
compiled template will be displayed.
The optional file argument directs where the traceback is sent;
it defaults to sys.stderr
.
[repeat=3 [,
number=1000000 ]]) |
This is a convenience function that calls the timeit() repeatedly, returning a list of results. The first argument specifies how many times to call timeit(). The second argument specifies the number argument for timeit().
[number=1000000 ]) |