summaryrefslogtreecommitdiffstats
path: root/Lib/timeit.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2016-10-18 15:06:56 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2016-10-18 15:06:56 (GMT)
commitf8fb82cd25739fc2fe67c5e6997af28f2a5590d3 (patch)
treebebffd97163aa6d225f9365157bd230324a4312e /Lib/timeit.py
parentbf9655854bec3e4cddc0580e815938f9f678cd23 (diff)
downloadcpython-f8fb82cd25739fc2fe67c5e6997af28f2a5590d3.zip
cpython-f8fb82cd25739fc2fe67c5e6997af28f2a5590d3.tar.gz
cpython-f8fb82cd25739fc2fe67c5e6997af28f2a5590d3.tar.bz2
timeit: start autorange with 1 iteration, not 10
Issue #28240: timeit autorange now uses a single loop iteration if the benchmark takes less than 10 seconds, instead of 10 iterations. "python3 -m timeit -s 'import time' 'time.sleep(1)'" now takes 4 seconds instead of 40 seconds.
Diffstat (limited to 'Lib/timeit.py')
-rw-r--r--Lib/timeit.py10
1 files changed, 6 insertions, 4 deletions
diff --git a/Lib/timeit.py b/Lib/timeit.py
index 2770efa..0040efd 100644
--- a/Lib/timeit.py
+++ b/Lib/timeit.py
@@ -218,7 +218,7 @@ class Timer:
If *callback* is given and is not None, it will be called after
each trial with two arguments: ``callback(number, time_taken)``.
"""
- for i in range(1, 10):
+ for i in range(0, 10):
number = 10**i
time_taken = self.timeit(number)
if callback:
@@ -318,8 +318,10 @@ def main(args=None, *, _wrap_timer=None):
callback = None
if verbose:
def callback(number, time_taken):
- msg = "{num} loops -> {secs:.{prec}g} secs"
- print(msg.format(num=number, secs=time_taken, prec=precision))
+ 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))
try:
number, _ = t.autorange(callback)
except:
@@ -333,7 +335,7 @@ def main(args=None, *, _wrap_timer=None):
best = min(r)
if verbose:
print("raw times:", " ".join(["%.*g" % (precision, x) for x in r]))
- print("%d loops," % number, end=' ')
+ print("%d loop%s," % (number, 's' if number != 1 else ''), end=' ')
usec = best * 1e6 / number
if time_unit is not None:
scale = units[time_unit]