mirror of
https://github.com/python/cpython
synced 2024-10-14 10:48:24 +00:00
gh-105052:update timeit function's description (#105060)
--------- Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
This commit is contained in:
parent
1ac64237e6
commit
7096a2be33
|
@ -86,9 +86,11 @@ The module defines three convenience functions and a public class:
|
||||||
.. versionchanged:: 3.7
|
.. versionchanged:: 3.7
|
||||||
Default value of *repeat* changed from 3 to 5.
|
Default value of *repeat* changed from 3 to 5.
|
||||||
|
|
||||||
|
|
||||||
.. function:: default_timer()
|
.. function:: default_timer()
|
||||||
|
|
||||||
The default timer, which is always :func:`time.perf_counter`.
|
The default timer, which is always time.perf_counter(), returns float seconds.
|
||||||
|
An alternative, time.perf_counter_ns, returns integer nanoseconds.
|
||||||
|
|
||||||
.. versionchanged:: 3.3
|
.. versionchanged:: 3.3
|
||||||
:func:`time.perf_counter` is now the default timer.
|
:func:`time.perf_counter` is now the default timer.
|
||||||
|
@ -124,7 +126,7 @@ The module defines three convenience functions and a public class:
|
||||||
|
|
||||||
Time *number* executions of the main statement. This executes the setup
|
Time *number* executions of the main statement. This executes the setup
|
||||||
statement once, and then returns the time it takes to execute the main
|
statement once, and then returns the time it takes to execute the main
|
||||||
statement a number of times, measured in seconds as a float.
|
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
|
The argument is the number of times through the loop, defaulting to one
|
||||||
million. The main statement, the setup statement and the timer function
|
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.
|
||||||
|
|
|
@ -50,9 +50,9 @@
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import gc
|
import gc
|
||||||
|
import itertools
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import itertools
|
|
||||||
|
|
||||||
__all__ = ["Timer", "timeit", "repeat", "default_timer"]
|
__all__ = ["Timer", "timeit", "repeat", "default_timer"]
|
||||||
|
|
||||||
|
@ -77,9 +77,11 @@ def inner(_it, _timer{init}):
|
||||||
return _t1 - _t0
|
return _t1 - _t0
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def reindent(src, indent):
|
def reindent(src, indent):
|
||||||
"""Helper to reindent a multi-line statement."""
|
"""Helper to reindent a multi-line statement."""
|
||||||
return src.replace("\n", "\n" + " "*indent)
|
return src.replace("\n", "\n" + " " * indent)
|
||||||
|
|
||||||
|
|
||||||
class Timer:
|
class Timer:
|
||||||
"""Class for timing execution speed of small code snippets.
|
"""Class for timing execution speed of small code snippets.
|
||||||
|
@ -166,7 +168,7 @@ def timeit(self, number=default_number):
|
||||||
|
|
||||||
To be precise, this executes the setup statement once, and
|
To be precise, this executes the setup statement once, and
|
||||||
then returns the time it takes to execute the main statement
|
then returns the time it takes to execute the main statement
|
||||||
a number of times, as a float measured in seconds. The
|
a number of times, as float seconds if using the default timer. The
|
||||||
argument is the number of times through the loop, defaulting
|
argument is the number of times through the loop, defaulting
|
||||||
to one million. The main statement, the setup statement and
|
to one million. The main statement, the setup statement and
|
||||||
the timer function to be used are passed to the constructor.
|
the timer function to be used are passed to the constructor.
|
||||||
|
@ -228,16 +230,19 @@ def autorange(self, callback=None):
|
||||||
return (number, time_taken)
|
return (number, time_taken)
|
||||||
i *= 10
|
i *= 10
|
||||||
|
|
||||||
|
|
||||||
def timeit(stmt="pass", setup="pass", timer=default_timer,
|
def timeit(stmt="pass", setup="pass", timer=default_timer,
|
||||||
number=default_number, globals=None):
|
number=default_number, globals=None):
|
||||||
"""Convenience function to create Timer object and call timeit method."""
|
"""Convenience function to create Timer object and call timeit method."""
|
||||||
return Timer(stmt, setup, timer, globals).timeit(number)
|
return Timer(stmt, setup, timer, globals).timeit(number)
|
||||||
|
|
||||||
|
|
||||||
def repeat(stmt="pass", setup="pass", timer=default_timer,
|
def repeat(stmt="pass", setup="pass", timer=default_timer,
|
||||||
repeat=default_repeat, number=default_number, globals=None):
|
repeat=default_repeat, number=default_number, globals=None):
|
||||||
"""Convenience function to create Timer object and call repeat method."""
|
"""Convenience function to create Timer object and call repeat method."""
|
||||||
return Timer(stmt, setup, timer, globals).repeat(repeat, number)
|
return Timer(stmt, setup, timer, globals).repeat(repeat, number)
|
||||||
|
|
||||||
|
|
||||||
def main(args=None, *, _wrap_timer=None):
|
def main(args=None, *, _wrap_timer=None):
|
||||||
"""Main program, used when run as a script.
|
"""Main program, used when run as a script.
|
||||||
|
|
||||||
|
@ -269,7 +274,7 @@ def main(args=None, *, _wrap_timer=None):
|
||||||
|
|
||||||
timer = default_timer
|
timer = default_timer
|
||||||
stmt = "\n".join(args) or "pass"
|
stmt = "\n".join(args) or "pass"
|
||||||
number = 0 # auto-determine
|
number = 0 # auto-determine
|
||||||
setup = []
|
setup = []
|
||||||
repeat = default_repeat
|
repeat = default_repeat
|
||||||
verbose = 0
|
verbose = 0
|
||||||
|
@ -286,7 +291,7 @@ def main(args=None, *, _wrap_timer=None):
|
||||||
time_unit = a
|
time_unit = a
|
||||||
else:
|
else:
|
||||||
print("Unrecognized unit. Please select nsec, usec, msec, or sec.",
|
print("Unrecognized unit. Please select nsec, usec, msec, or sec.",
|
||||||
file=sys.stderr)
|
file=sys.stderr)
|
||||||
return 2
|
return 2
|
||||||
if o in ("-r", "--repeat"):
|
if o in ("-r", "--repeat"):
|
||||||
repeat = int(a)
|
repeat = int(a)
|
||||||
|
@ -320,7 +325,7 @@ def callback(number, time_taken):
|
||||||
msg = "{num} loop{s} -> {secs:.{prec}g} secs"
|
msg = "{num} loop{s} -> {secs:.{prec}g} secs"
|
||||||
plural = (number != 1)
|
plural = (number != 1)
|
||||||
print(msg.format(num=number, s='s' if plural else '',
|
print(msg.format(num=number, s='s' if plural else '',
|
||||||
secs=time_taken, prec=precision))
|
secs=time_taken, prec=precision))
|
||||||
try:
|
try:
|
||||||
number, _ = t.autorange(callback)
|
number, _ = t.autorange(callback)
|
||||||
except:
|
except:
|
||||||
|
@ -371,5 +376,6 @@ def format_time(dt):
|
||||||
UserWarning, '', 0)
|
UserWarning, '', 0)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
sys.exit(main())
|
sys.exit(main())
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Update ``timeit`` doc to specify that time in seconds is just the default.
|
Loading…
Reference in a new issue