10000 gh-105052:update timeit function's description by Cherrymelon · Pull Request #105060 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-105052:update timeit function's description #105060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 3 additions & 1 deletion Doc/library/timeit.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ A callable can also be passed from the :ref:`python-interface`::
Note however that :func:`.timeit` will automatically determine the number of
repetitions only when the command-line interface is used. In the
:ref:`timeit-examples` section you can find more advanced examples.
Especially, the time unit can be changed by modifying :func:`default_timer`
or passing other timer in function.


.. _python-interface:
Expand Down Expand Up @@ -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
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, measured in seconds as a float by 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.
Expand Down
19 changes: 13 additions & 6 deletions Lib/timeit.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@
"""

import gc
import itertools
import sys
import time
import itertools

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

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


def reindent(src, indent):
"""Helper to reindent a multi-line statement."""
return src.replace("\n", "\n" + " "*indent)
return src.replace("\n", "\n" + " " * indent)


class Timer:
"""Class for timing execution speed of small code snippets.
Expand Down Expand Up @@ -166,7 +168,8 @@ 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 a float measured in seconds. The
a number of times, as a float measured in seconds if using default
timer.Thus,time unit could be changed if giving other 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.
Expand Down Expand Up @@ -228,16 +231,19 @@ def autorange(self, callback=None):
return (number, time_taken)
i *= 10


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


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


def main(args=None, *, _wrap_timer=None):
"""Main program, used when run as a script.

Expand Down Expand Up @@ -269,7 +275,7 @@ def main(args=None, *, _wrap_timer=None):

timer = default_timer
stmt = "\n".join(args) or "pass"
number = 0 # auto-determine
number = 0 # auto-determine
setup = []
repeat = default_repeat
verbose = 0
Expand All @@ -286,7 +292,7 @@ def main(args=None, *, _wrap_timer=None):
time_unit = a
else:
print("Unrecognized unit. Please select nsec, usec, msec, or sec.",
file=sys.stderr)
file=sys.stderr)
return 2
if o in ("-r", "--repeat"):
repeat = int(a)
Expand Down Expand Up @@ -320,7 +326,7 @@ def callback(number, time_taken):
msg = "{num} loop{s} -> {secs:.{prec}g} secs"
plural = (number != 1)
print(msg.format(num=number, s='s' if plural else '',
secs=time_taken, prec=precision))
secs=time_taken, prec=precision))
try:
number, _ = t.autorange(callback)
except:
Expand Down Expand Up @@ -371,5 +377,6 @@ def format_time(dt):
UserWarning, '', 0)
return None


if __name__ == "__main__":
sys.exit(main())
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
timeit doc update
0