rest25/library/timeit.rst => rest262/library/timeit.rst
15This module provides a simple way to time small bits of Python code. It has both
16command line as well as callable interfaces.  It avoids a number of common traps
17for measuring execution times.  See also Tim Peters' introduction to the
18"Algorithms" chapter in the Python Cookbook, published by O'Reilly.
19
20The module defines the following public class:
21
22
n23-.. class:: Timer([stmt=``'pass'`` [, setup=``'pass'`` [, timer=<timer function>]]])
n23+.. class:: Timer([stmt='pass' [, setup='pass' [, timer=<timer function>]]])
24
25   Class for timing execution speed of small code snippets.
26
27   The constructor takes a statement to be timed, an additional statement used for
28   setup, and a timer function.  Both statements default to ``'pass'``; the timer
29   function is platform-dependent (see the module doc string).  The statements may
30   contain newlines, as long as they don't contain multi-line string literals.
31
32   To measure the execution time of the first statement, use the :meth:`timeit`
33   method.  The :meth:`repeat` method is a convenience to call :meth:`timeit`
34   multiple times and return a list of results.
35
n36+   .. versionchanged:: 2.6
37+      The *stmt* and *setup* parameters can now also take objects that are callable
38+      without arguments. This will embed calls to them in a timer function that will
39+      then be executed by :meth:`timeit`.  Note that the timing overhead is a little
40+      larger in this case because of the extra function calls.
36
n42+ 
37-.. method:: Timer.print_exc([file=:const:`None`])
43+.. method:: Timer.print_exc([file=None])
38
39   Helper to print a traceback from the timed code.
40
41   Typical use::
42
43      t = Timer(...)       # outside the try/except
44      try:
45          t.timeit(...)    # or t.repeat(...)
46      except:
47          t.print_exc()
48
49   The advantage over the standard traceback is that source lines in the compiled
50   template will be displayed. The optional *file* argument directs where the
51   traceback is sent; it defaults to ``sys.stderr``.
52
53
n54-.. method:: Timer.repeat([repeat\ ``=3`` [, number\ ``=1000000``]])
n60+.. method:: Timer.repeat([repeat=3 [, number=1000000]])
55
56   Call :meth:`timeit` a few times.
57
58   This is a convenience function that calls the :meth:`timeit` repeatedly,
59   returning a list of results.  The first argument specifies how many times to
60   call :meth:`timeit`.  The second argument specifies the *number* argument for
61   :func:`timeit`.
62
67      lowest value gives a lower bound for how fast your machine can run the given
68      code snippet; higher values in the result vector are typically not caused by
69      variability in Python's speed, but by other processes interfering with your
70      timing accuracy.  So the :func:`min` of the result is probably the only number
71      you should be interested in.  After that, you should look at the entire vector
72      and apply common sense rather than statistics.
73
74
n75-.. method:: Timer.timeit([number\ ``=1000000``])
n81+.. method:: Timer.timeit([number=1000000])
76
77   Time *number* executions of the main statement. This executes the setup
78   statement once, and then returns the time it takes to execute the main statement
79   a number of times, measured in seconds as a float.  The argument is the number
80   of times through the loop, defaulting to one million.  The main statement, the
81   setup statement and the timer function to be used are passed to the constructor.
82
83   .. note::
84
n85-      By default, :meth:`timeit` temporarily turns off garbage collection during the
n91+      By default, :meth:`timeit` temporarily turns off :term:`garbage collection`
86-      timing.  The advantage of this approach is that it makes independent timings
92+      during the timing.  The advantage of this approach is that it makes
87-      more comparable.  This disadvantage is that GC may be an important component of
93+      independent timings more comparable.  This disadvantage is that GC may be
88-      the performance of the function being measured.  If so, GC can be re-enabled as
94+      an important component of the performance of the function being measured.
89-      the first statement in the *setup* string.  For example::
95+      If so, GC can be re-enabled as the first statement in the *setup* string.
96+      For example::
90
91         timeit.Timer('for i in xrange(10): oct(i)', 'gc.enable()').timeit()
n99+ 
100+Starting with version 2.6, the module also defines two convenience functions:
101+ 
102+ 
103+.. function:: repeat(stmt[, setup[, timer[, repeat=3 [, number=1000000]]]])
104+ 
105+   Create a :class:`Timer` instance with the given statement, setup code and timer
106+   function and run its :meth:`repeat` method with the given repeat count and
107+   *number* executions.
108+ 
109+   .. versionadded:: 2.6
110+ 
111+ 
112+.. function:: timeit(stmt[, setup[, timer[, number=1000000]]])
113+ 
114+   Create a :class:`Timer` instance with the given statement, setup code and timer
115+   function and run its :meth:`timeit` method with *number* executions.
116+ 
117+   .. versionadded:: 2.6
92
93
94Command Line Interface
95----------------------
96
97When called as a program from the command line, the following form is used::
98
t99-   python timeit.py [-n N] [-r N] [-s S] [-t] [-c] [-h] [statement ...]
t125+   python -m timeit [-n N] [-r N] [-s S] [-t] [-c] [-h] [statement ...]
100
101where the following options are understood:
102
103-n N/:option:`--number=N`
104   how many times to execute 'statement'
105
106-r N/:option:`--repeat=N`
107   how many times to repeat the timer (default 3)
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op