8000 gh-105052:update timeit function's description (#105060) · python/cpython@7096a2b · GitHub
[go: up one dir, main page]

Skip to content

Commit 7096a2b

Browse files
gh-105052:update timeit function's description (#105060)
--------- Co-authored-by: Terry Jan Reedy <tjreedy@u 10000 del.edu>
1 parent 1ac6423 commit 7096a2b

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

Doc/library/timeit.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ The module defines three convenience functions and a public class:
8686
.. versionchanged:: 3.7
8787
Default value of *repeat* changed from 3 to 5.
8888

89+
8990
.. function:: default_timer()
9091

91-
The default timer, which is always :func:`time.perf_counter`.
92+
The default timer, which is always time.perf_counter(), returns float seconds.
93+
An alternative, time.perf_counter_ns, returns integer nanoseconds.
9294

9395
.. versionchanged:: 3.3
9496
:func:`time.perf_counter` is now the default timer.
@@ -124,7 +126,7 @@ The module defines three convenience functions and a public class:
124126

125127
Time *number* executions of the main statement. This executes the setup
126128
statement once, and then returns the time it takes to execute the main
127-
statement a number of times, measured in seconds as a float.
129+
statement a number of times. The default timer returns seconds as a float.
128130
The argument is the number of times through the loop, defaulting to one
129131
million. The main statement, the setup statement and the timer function
130132
to be used are passed to the constructor.

Lib/timeit.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@
5050
"""
5151

5252
import gc
53+
import itertools
5354
import sys
5455
import time
55-
import itertools
5656

5757
__all__ = ["Timer", "timeit", "repeat", "default_timer"]
5858

@@ -77,9 +77,11 @@ def inner(_it, _timer{init}):
7777
return _t1 - _t0
7878
"""
7979

80+
8081
def reindent(src, indent):
8182
"""Helper to reindent a multi-line statement."""
82-
return src.replace("\n", "\n" + " "*indent)
83+
return src.replace("\n", "\n" + " " * indent)
84+
8385

8486
class Timer:
8587
"""Class for timing execution speed of small code snippets.
@@ -166,7 +168,7 @@ def timeit(self, number=default_number):
166168
167169
To be precise, this executes the setup statement once, and
168170
then returns the time it takes to execute the main statement
169-
a number of times, as a float measured in seconds. The
171+
a number of times, as float seconds if using the default timer. The
170172
argument is the number of times through the loop, defaulting
171173
to one million. The main statement, the setup statement and
172174
the timer function to be used are passed to the constructor.
@@ -228,16 +230,19 @@ def autorange(self, callback=None):
228230
return (number, time_taken)
229231
i *= 10
230232

233+
231234
def timeit(stmt="pass", setup="pass", timer=default_timer,
232235
number=default_number, globals=None):
233236
"""Convenience function to create Timer object and call timeit method."""
234237
return Timer(stmt, setup, timer, globals).timeit(number)
235238

239+
236240
def repeat(stmt="pass", setup="pass", timer=default_timer,
237241
repeat=default_repeat, number=default_number, globals=None):
238242
"""Convenience function to create Timer object and call repeat method."""
239243
return Timer(stmt, setup, timer, globals).repeat(repeat, number)
240244

245+
241246
def main(args=None, *, _wrap_timer=None):
242247
"""Main program, used when run as a script.
243248
@@ -269,7 +274,7 @@ def main(args=None, *, _wrap_timer=None):
269274

270275
timer = default_timer
271276
stmt = "\n".join(args) or "pass"
272-
number = 0 # auto-determine
277+
number = 0 # auto-determine
273278
setup = []
274279
repeat = default_repeat
275280
verbose = 0
@@ -286,7 +291,7 @@ def main(args=None, *, _wrap_timer=None):
286291
time_unit = a
287292
else:
288293
print("Unrecognized unit. Please select nsec, usec, msec, or sec.",
289-
file=sys.stderr)
294+
file=sys.stderr)
290295
return 2
291296
if o in ("-r", "--repeat"):
292297
repeat = int(a)
@@ -320,7 +325,7 @@ def callback(number, time_taken):
320325
msg = "{num} loop{s} -> {secs:.{prec}g} secs"
321326
plural = (number != 1)
322327
print(msg.format(num=number, s='s' if plural else '',
323-
secs=time_taken, prec=precision))
328+
secs=time_taken, prec=precision))
324329
try:
325330
number, _ = t.autorange(callback)
326331
except:
@@ -371,5 +376,6 @@ def format_time(dt):
371376
UserWarning, '', 0)
372377
return None
373378

379+
374380
if __name__ == "__main__":
375381
sys.exit(main())
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update ``timeit`` doc to specify that time in seconds is just the default.

0 commit comments

Comments
 (0)
0