From 619dfe22ba05360a47757352eb2c3d640dbe9cf7 Mon Sep 17 00:00:00 2001 From: Joey Rahme Date: Tue, 1 Dec 2026 12:44:14 -0500 Subject: [PATCH] gh-114665: Clarify that timeit setup runs once per timing call The docs and CLI help said setup ran "once initially" or "only once at the beginning". Setup actually runs once at the start of every Timer.timeit() call, so repeat(), autorange(), and the command-line interface run it more than once. Assisted-by: Grok Bot --- Doc/library/timeit.rst | 19 ++++++++++++++++--- Lib/timeit.py | 10 +++++++--- ...-12-01-12-42-00.gh-issue-114665.K7mNpQ.rst | 4 ++++ 3 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 Misc/NEWS.d/next/Documentation/2026-12-01-12-42-00.gh-issue-114665.K7mNpQ.rst diff --git a/Doc/library/timeit.rst b/Doc/library/timeit.rst index 09da385bfdf79a..74272f33d57206 100644 --- a/Doc/library/timeit.rst +++ b/Doc/library/timeit.rst @@ -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 @@ -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:: @@ -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 @@ -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 diff --git a/Lib/timeit.py b/Lib/timeit.py index 01bdfd901e30d5..f0ace1cf0d89fb 100644 --- a/Lib/timeit.py +++ b/Lib/timeit.py @@ -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() @@ -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')", ) diff --git a/Misc/NEWS.d/next/Documentation/2026-12-01-12-42-00.gh-issue-114665.K7mNpQ.rst b/Misc/NEWS.d/next/Documentation/2026-12-01-12-42-00.gh-issue-114665.K7mNpQ.rst new file mode 100644 index 00000000000000..5c75fd7beaf8a2 --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2026-12-01-12-42-00.gh-issue-114665.K7mNpQ.rst @@ -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.