New in version 2.3.
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:
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.
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
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().