:mod:`timeit` --- Measure execution time of small code snippets
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:
Class for timing execution speed of small code snippets.
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). stmt and setup
may also contain multiple statements separated by ;
or newlines, as long as
they don't contain multi-line string literals.
To measure the execution time of the first statement, use the :meth:`timeit` method. The :meth:`repeat` method is a convenience to call :meth:`timeit` multiple times and return a list of results.
The stmt and setup parameters can also take objects that are callable without arguments. This will embed calls to them in a timer function that will then be executed by :meth:`timeit`. Note that the timing overhead is a little larger in this case because of the extra function calls.
The module also defines two convenience functions:
Command Line Interface
When called as a program from the command line, the following form is used:
python -m timeit [-n N] [-r N] [-s S] [-t] [-c] [-h] [statement ...]
where the following options are understood:
- -n N/:option:`--number=N`
- how many times to execute 'statement'
- -r N/:option:`--repeat=N`
- how many times to repeat the timer (default 3)
- -s S/:option:`--setup=S`
- statement to be executed once initially (default
'pass'
) - -t/:option:`--time`
- use :func:`time.time` (default on all platforms but Windows)
- -c/:option:`--clock`
- use :func:`time.clock` (default on Windows)
- -v/:option:`--verbose`
- print raw timing results; repeat for more digits precision
- -h/:option:`--help`
- print a short usage message and exit
A multi-line statement may be given by specifying each line as a separate statement argument; indented lines are possible by enclosing an argument in quotes and using leading spaces. Multiple :option:`-s` options are treated similarly.
If :option:`-n` is not given, a suitable number of loops is calculated by trying successive powers of 10 until the total time is at least 0.2 seconds.
The default timer function is platform dependent. On Windows, :func:`time.clock` has microsecond granularity but :func:`time.time`'s granularity is 1/60th of a second; on Unix, :func:`time.clock` has 1/100th of a second granularity and :func:`time.time` is much more precise. On either platform, the default timer functions measure wall clock time, not the CPU time. This means that other processes running on the same computer may interfere with the timing. The best thing to do when accurate timing is necessary is to repeat the timing a few times and use the best time. The :option:`-r` option is good for this; the default of 3 repetitions is probably enough in most cases. On Unix, you can use :func:`time.clock` to measure CPU time.
Note
There is a certain baseline overhead associated with executing a pass statement. The code here doesn't try to hide it, but you should be aware of it. The baseline overhead can be measured by invoking the program without arguments.
The baseline overhead differs between Python versions! Also, to fairly compare
older Python versions to Python 2.3, you may want to use Python's :option:`-O`
option for the older versions to avoid timing SET_LINENO
instructions.
Examples
Here are two example sessions (one using the command line, one using the module interface) that compare the cost of using :func:`hasattr` vs. :keyword:`try`/:keyword:`except` to test for missing and present object attributes.
% timeit.py 'try:' ' str.__bool__' 'except AttributeError:' ' pass'
100000 loops, best of 3: 15.7 usec per loop
% timeit.py 'if hasattr(str, "__bool__"): pass'
100000 loops, best of 3: 4.26 usec per loop
% timeit.py 'try:' ' int.__bool__' 'except AttributeError:' ' pass'
1000000 loops, best of 3: 1.43 usec per loop
% timeit.py 'if hasattr(int, "__bool__"): pass'
100000 loops, best of 3: 2.23 usec per loop
>>> import timeit
>>> s = """\
... try:
... str.__bool__
... except AttributeError:
... pass
... """
>>> t = timeit.Timer(stmt=s)
>>> print("%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000))
17.09 usec/pass
>>> s = """\
... if hasattr(str, '__bool__'): pass
... """
>>> t = timeit.Timer(stmt=s)
>>> print("%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000))
4.85 usec/pass
>>> s = """\
... try:
... int.__bool__
... except AttributeError:
... pass
... """
>>> t = timeit.Timer(stmt=s)
>>> print("%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000))
1.97 usec/pass
>>> s = """\
... if hasattr(int, '__bool__'): pass
... """
>>> t = timeit.Timer(stmt=s)
>>> print("%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000))
3.15 usec/pass
To give the :mod:`timeit` module access to functions you define, you can pass a
setup
parameter which contains an import statement:
def test():
"Stupid test function"
L = [i for i in range(100)]
if __name__=='__main__':
from timeit import Timer
t = Timer("test()", "from __main__ import test")
print(t.timeit())