Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions Doc/library/timeit.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ The module defines three convenience functions and a public class:
methods to call :meth:`.timeit` multiple times.

The execution time of *setup* is excluded from the overall timed execution run.
*setup* itself runs once at the start of every :meth:`.timeit` call,
before the timed loop. It does not run only once for the lifetime of a
:class:`Timer`. Because :meth:`.repeat` and :meth:`.autorange` call
:meth:`.timeit` more than once, they run *setup* once per call.

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
Expand All @@ -129,7 +133,9 @@ The module defines three convenience functions and a public class:
statement a number of times. The default timer returns seconds as a float.
The argument is the number of times through the loop, defaulting to one
million. The main statement, the setup statement and the timer function
to be used are passed to the constructor.
to be used are passed to the constructor. Each call to :meth:`.timeit`,
including those made by :meth:`.repeat` and :meth:`.autorange`, runs the
setup statement again.

.. note::

Expand Down Expand Up @@ -227,7 +233,12 @@ Where the following options are understood:

.. option:: -s S, --setup=S

statement to be executed once initially (default ``pass``)
statement to be executed before each timed run (default ``pass``)

The setup statement is run once per :meth:`~timeit.Timer.timeit` call: once
for each :option:`-r` repetition, and also once for each trial used to
choose the loop count when :option:`-n` is omitted. Its execution time is
not included in the reported timings.

.. option:: -p, --process

Expand Down Expand Up @@ -288,7 +299,9 @@ most cases. You can use :func:`time.process_time` to measure CPU time.
Examples
--------

It is possible to provide a setup statement that is executed only once at the beginning:
A setup statement can prepare values used by the timed statement. On the
command line, that setup is run before every timing call, not only once at the
beginning of the command (see :class:`Timer`):

.. code-block:: shell-session

Expand Down
10 changes: 7 additions & 3 deletions Lib/timeit.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,13 @@ def timeit(self, number=default_number):

To be precise, this executes the setup statement once, and
then returns the time it takes to execute the main statement
a number of times, as float seconds if using the default timer. The
argument is the number of times through the loop, defaulting
a number of times, as float seconds if using the default timer.
The argument is the number of times through the loop, defaulting
to one million. The main statement, the setup statement and
the timer function to be used are passed to the constructor.
Methods that call timeit() more than once, such as repeat()
and autorange(), therefore run the setup statement once per
call.
"""
it = itertools.repeat(None, number)
gcold = gc.isenabled()
Expand Down Expand Up @@ -291,7 +294,8 @@ def main(args=None, *, _wrap_timer=None):
"--setup",
action="append",
default=[],
help="statement to be executed once initially. "
help="statement to be executed once before each timed run "
"(also once per trial when -n is omitted). "
"Execution time of this setup statement is NOT timed. "
"(default 'pass')",
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
The :mod:`timeit` documentation and command-line help now state that the
*setup* statement is executed once per :meth:`~timeit.Timer.timeit` call,
not only once at the start of a :meth:`~timeit.Timer.repeat` sequence or a
command-line run.
Loading