PyDocs

Nav

Debugging and profiling

Timer

timeit

class (source)
  • Timer(stmt='pass', setup='pass', timer=<timer function>, globals=None)
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
module doc string).  If 'globals' is specified, the code will be
executed within that namespace (as opposed to inside timeit's
namespace).

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.

The statements may contain newlines, as long as they don't contain
multi-line string literals.
Changed in 3.5
  • 3.5: The optional globals parameter was added.
Members of Timer

a

autorange
Return the number of loops and time taken so that total time >= target_time (default is 0.2 seconds).

p

print_exc
Helper to print a traceback from the timed code.

r

repeat
Call timeit() a few times.

t

timeit
Time 'number' executions of the main statement.
1 Example
>>> import timeit
>>> t = timeit.Timer('char in text', setup='text = "sample string"; char = "g"')
>>> t.timeit()
0.3955516149999312
>>> t.repeat()
[0.40183617287970225, 0.37027556854118704, 0.38344867356679524, 0.3712595970846668, 0.37866875250654886]
from the Python docs
See Also
No see-alsos for timeit/Timer
0 Notes
No notes for Timer