timeit.Timer

class timeit.Timer(stmt='pass', setup='pass', timer=<built-in function time>)[source]

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).

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.

Methods

__init__([stmt, setup, timer]) Constructor.
print_exc([file]) Helper to print a traceback from the timed code.
repeat([repeat, number]) Call timeit() a few times.
timeit([number]) Time ‘number’ executions of the main statement.