mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
parent
3d7feb9ac2
commit
61de57f175
2 changed files with 40 additions and 27 deletions
|
@ -297,7 +297,7 @@ class TestTimeit(unittest.TestCase):
|
||||||
s = self.run_main(switches=['-v'])
|
s = self.run_main(switches=['-v'])
|
||||||
self.assertEqual(s, dedent("""\
|
self.assertEqual(s, dedent("""\
|
||||||
1 loop -> 1 secs
|
1 loop -> 1 secs
|
||||||
raw times: 1 1 1 1 1
|
raw times: 1 sec, 1 sec, 1 sec, 1 sec, 1 sec
|
||||||
1 loop, best of 5: 1 sec per loop
|
1 loop, best of 5: 1 sec per loop
|
||||||
"""))
|
"""))
|
||||||
|
|
||||||
|
@ -309,7 +309,7 @@ class TestTimeit(unittest.TestCase):
|
||||||
100 loops -> 0.005 secs
|
100 loops -> 0.005 secs
|
||||||
1000 loops -> 0.05 secs
|
1000 loops -> 0.05 secs
|
||||||
10000 loops -> 0.5 secs
|
10000 loops -> 0.5 secs
|
||||||
raw times: 0.5 0.5 0.5 0.5 0.5
|
raw times: 500 msec, 500 msec, 500 msec, 500 msec, 500 msec
|
||||||
10000 loops, best of 5: 50 usec per loop
|
10000 loops, best of 5: 50 usec per loop
|
||||||
"""))
|
"""))
|
||||||
|
|
||||||
|
|
|
@ -264,6 +264,7 @@ def main(args=None, *, _wrap_timer=None):
|
||||||
print(err)
|
print(err)
|
||||||
print("use -h/--help for command line help")
|
print("use -h/--help for command line help")
|
||||||
return 2
|
return 2
|
||||||
|
|
||||||
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
|
||||||
|
@ -271,7 +272,7 @@ def main(args=None, *, _wrap_timer=None):
|
||||||
repeat = default_repeat
|
repeat = default_repeat
|
||||||
verbose = 0
|
verbose = 0
|
||||||
time_unit = None
|
time_unit = None
|
||||||
units = {"usec": 1, "msec": 1e3, "sec": 1e6}
|
units = {"usec": 1e-6, "msec": 1e-3, "sec": 1.0}
|
||||||
precision = 3
|
precision = 3
|
||||||
for o, a in opts:
|
for o, a in opts:
|
||||||
if o in ("-n", "--number"):
|
if o in ("-n", "--number"):
|
||||||
|
@ -299,6 +300,7 @@ def main(args=None, *, _wrap_timer=None):
|
||||||
print(__doc__, end=' ')
|
print(__doc__, end=' ')
|
||||||
return 0
|
return 0
|
||||||
setup = "\n".join(setup) or "pass"
|
setup = "\n".join(setup) or "pass"
|
||||||
|
|
||||||
# Include the current directory, so that local imports work (sys.path
|
# Include the current directory, so that local imports work (sys.path
|
||||||
# contains the directory of this script, rather than the current
|
# contains the directory of this script, rather than the current
|
||||||
# directory)
|
# directory)
|
||||||
|
@ -306,6 +308,7 @@ def main(args=None, *, _wrap_timer=None):
|
||||||
sys.path.insert(0, os.curdir)
|
sys.path.insert(0, os.curdir)
|
||||||
if _wrap_timer is not None:
|
if _wrap_timer is not None:
|
||||||
timer = _wrap_timer(timer)
|
timer = _wrap_timer(timer)
|
||||||
|
|
||||||
t = Timer(stmt, setup, timer)
|
t = Timer(stmt, setup, timer)
|
||||||
if number == 0:
|
if number == 0:
|
||||||
# determine number so that 0.2 <= total time < 2.0
|
# determine number so that 0.2 <= total time < 2.0
|
||||||
|
@ -321,36 +324,46 @@ def main(args=None, *, _wrap_timer=None):
|
||||||
except:
|
except:
|
||||||
t.print_exc()
|
t.print_exc()
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
try:
|
try:
|
||||||
r = t.repeat(repeat, number)
|
raw_timings = t.repeat(repeat, number)
|
||||||
except:
|
except:
|
||||||
t.print_exc()
|
t.print_exc()
|
||||||
return 1
|
return 1
|
||||||
best = min(r)
|
|
||||||
if verbose:
|
def format_time(dt):
|
||||||
print("raw times:", " ".join(["%.*g" % (precision, x) for x in r]))
|
unit = time_unit
|
||||||
print("%d loop%s," % (number, 's' if number != 1 else ''), end=' ')
|
|
||||||
usec = best * 1e6 / number
|
if unit is not None:
|
||||||
if time_unit is not None:
|
scale = units[unit]
|
||||||
scale = units[time_unit]
|
|
||||||
else:
|
else:
|
||||||
scales = [(scale, unit) for unit, scale in units.items()]
|
scales = [(scale, unit) for unit, scale in units.items()]
|
||||||
scales.sort(reverse=True)
|
scales.sort(reverse=True)
|
||||||
for scale, time_unit in scales:
|
for scale, unit in scales:
|
||||||
if usec >= scale:
|
if dt >= scale:
|
||||||
break
|
break
|
||||||
print("best of %d: %.*g %s per loop" % (repeat, precision,
|
|
||||||
usec/scale, time_unit))
|
return "%.*g %s" % (precision, dt / scale, unit)
|
||||||
best = min(r)
|
|
||||||
usec = best * 1e6 / number
|
if verbose:
|
||||||
worst = max(r)
|
print("raw times: %s" % ", ".join(map(format_time, raw_timings)))
|
||||||
|
|
||||||
|
timings = [dt / number for dt in raw_timings]
|
||||||
|
|
||||||
|
best = min(timings)
|
||||||
|
print("%d loop%s, best of %d: %s per loop"
|
||||||
|
% (number, 's' if number != 1 else '',
|
||||||
|
repeat, format_time(best)))
|
||||||
|
|
||||||
|
best = min(timings)
|
||||||
|
worst = max(timings)
|
||||||
if worst >= best * 4:
|
if worst >= best * 4:
|
||||||
usec = worst * 1e6 / number
|
|
||||||
import warnings
|
import warnings
|
||||||
warnings.warn_explicit(
|
warnings.warn_explicit("The test results are likely unreliable. "
|
||||||
"The test results are likely unreliable. The worst\n"
|
"The worst time (%s) was more than four times "
|
||||||
"time (%.*g %s) was more than four times slower than the best time." %
|
"slower than the best time (%s)."
|
||||||
(precision, usec/scale, time_unit),
|
% (precision,
|
||||||
|
format_time(worst), format_time(best)),
|
||||||
UserWarning, '', 0)
|
UserWarning, '', 0)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue