summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2016-10-23 12:17:05 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2016-10-23 12:17:05 (GMT)
commitd3ff784f2d6ffaeee0e9aa1999d2e8ceed47ae82 (patch)
tree2198560cad1ef6289313a9b808997c803ebe03db /Lib
parentf8d7d41507d8c0855b58c9be14011381d03c5cde (diff)
downloadcpython-d3ff784f2d6ffaeee0e9aa1999d2e8ceed47ae82.zip
cpython-d3ff784f2d6ffaeee0e9aa1999d2e8ceed47ae82.tar.gz
cpython-d3ff784f2d6ffaeee0e9aa1999d2e8ceed47ae82.tar.bz2
Issue #28469: timeit now uses the sequence 1, 2, 5, 10, 20, 50,... instead
of 1, 10, 100,... for autoranging.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_timeit.py63
-rw-r--r--Lib/timeit.py25
2 files changed, 51 insertions, 37 deletions
diff --git a/Lib/test/test_timeit.py b/Lib/test/test_timeit.py
index a4298b5..8d4746a 100644
--- a/Lib/test/test_timeit.py
+++ b/Lib/test/test_timeit.py
@@ -256,7 +256,7 @@ class TestTimeit(unittest.TestCase):
def test_main_milliseconds(self):
s = self.run_main(seconds_per_increment=0.0055)
- self.assertEqual(s, "100 loops, best of 5: 5.5 msec per loop\n")
+ self.assertEqual(s, "50 loops, best of 5: 5.5 msec per loop\n")
def test_main_microseconds(self):
s = self.run_main(seconds_per_increment=0.0000025, switches=['-n100'])
@@ -304,35 +304,43 @@ class TestTimeit(unittest.TestCase):
"""))
def test_main_very_verbose(self):
- s = self.run_main(seconds_per_increment=0.000050, switches=['-vv'])
+ s = self.run_main(seconds_per_increment=0.000_030, switches=['-vv'])
self.assertEqual(s, dedent("""\
- 1 loop -> 5e-05 secs
- 10 loops -> 0.0005 secs
- 100 loops -> 0.005 secs
- 1000 loops -> 0.05 secs
- 10000 loops -> 0.5 secs
-
- raw times: 500 msec, 500 msec, 500 msec, 500 msec, 500 msec
-
- 10000 loops, best of 5: 50 usec per loop
+ 1 loop -> 3e-05 secs
+ 2 loops -> 6e-05 secs
+ 5 loops -> 0.00015 secs
+ 10 loops -> 0.0003 secs
+ 20 loops -> 0.0006 secs
+ 50 loops -> 0.0015 secs
+ 100 loops -> 0.003 secs
+ 200 loops -> 0.006 secs
+ 500 loops -> 0.015 secs
+ 1000 loops -> 0.03 secs
+ 2000 loops -> 0.06 secs
+ 5000 loops -> 0.15 secs
+ 10000 loops -> 0.3 secs
+
+ raw times: 300 msec, 300 msec, 300 msec, 300 msec, 300 msec
+
+ 10000 loops, best of 5: 30 usec per loop
"""))
def test_main_with_time_unit(self):
- unit_sec = self.run_main(seconds_per_increment=0.002,
+ unit_sec = self.run_main(seconds_per_increment=0.003,
switches=['-u', 'sec'])
self.assertEqual(unit_sec,
- "100 loops, best of 5: 0.002 sec per loop\n")
- unit_msec = self.run_main(seconds_per_increment=0.002,
+ "100 loops, best of 5: 0.003 sec per loop\n")
+ unit_msec = self.run_main(seconds_per_increment=0.003,
switches=['-u', 'msec'])
self.assertEqual(unit_msec,
- "100 loops, best of 5: 2 msec per loop\n")
- unit_usec = self.run_main(seconds_per_increment=0.002,
+ "100 loops, best of 5: 3 msec per loop\n")
+ unit_usec = self.run_main(seconds_per_increment=0.003,
switches=['-u', 'usec'])
self.assertEqual(unit_usec,
- "100 loops, best of 5: 2e+03 usec per loop\n")
+ "100 loops, best of 5: 3e+03 usec per loop\n")
# Test invalid unit input
with captured_stderr() as error_stringio:
- invalid = self.run_main(seconds_per_increment=0.002,
+ invalid = self.run_main(seconds_per_increment=0.003,
switches=['-u', 'parsec'])
self.assertEqual(error_stringio.getvalue(),
"Unrecognized unit. Please select nsec, usec, msec, or sec.\n")
@@ -347,15 +355,15 @@ class TestTimeit(unittest.TestCase):
s = self.run_main(switches=['-n1', '1/0'])
self.assert_exc_string(error_stringio.getvalue(), 'ZeroDivisionError')
- def autorange(self, seconds_per_increment=0.001, callback=None):
+ def autorange(self, seconds_per_increment=1/1024, callback=None):
timer = FakeTimer(seconds_per_increment=seconds_per_increment)
t = timeit.Timer(stmt=self.fake_stmt, setup=self.fake_setup, timer=timer)
return t.autorange(callback)
def test_autorange(self):
num_loops, time_taken = self.autorange()
- self.assertEqual(num_loops, 1000)
- self.assertEqual(time_taken, 1.0)
+ self.assertEqual(num_loops, 500)
+ self.assertEqual(time_taken, 500/1024)
def test_autorange_second(self):
num_loops, time_taken = self.autorange(seconds_per_increment=1.0)
@@ -367,12 +375,17 @@ class TestTimeit(unittest.TestCase):
print("{} {:.3f}".format(a, b))
with captured_stdout() as s:
num_loops, time_taken = self.autorange(callback=callback)
- self.assertEqual(num_loops, 1000)
- self.assertEqual(time_taken, 1.0)
+ self.assertEqual(num_loops, 500)
+ self.assertEqual(time_taken, 500/1024)
expected = ('1 0.001\n'
+ '2 0.002\n'
+ '5 0.005\n'
'10 0.010\n'
- '100 0.100\n'
- '1000 1.000\n')
+ '20 0.020\n'
+ '50 0.049\n'
+ '100 0.098\n'
+ '200 0.195\n'
+ '500 0.488\n')
self.assertEqual(s.getvalue(), expected)
diff --git a/Lib/timeit.py b/Lib/timeit.py
index 0b5061f..38c2b1f 100644
--- a/Lib/timeit.py
+++ b/Lib/timeit.py
@@ -208,22 +208,23 @@ class Timer:
def autorange(self, callback=None):
"""Return the number of loops so that total time >= 0.2.
- Calls the timeit method with *number* set to successive powers of
- ten (10, 100, 1000, ...) up to a maximum of one billion, until
- the time taken is at least 0.2 second, or the maximum is reached.
- Returns ``(number, time_taken)``.
+ Calls the timeit method with increasing numbers from the sequence
+ 1, 2, 5, 10, 20, 50, ... until the time taken is at least 0.2
+ second. Returns (number, time_taken).
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(0, 10):
- number = 10**i
- time_taken = self.timeit(number)
- if callback:
- callback(number, time_taken)
- if time_taken >= 0.2:
- break
- return (number, time_taken)
+ i = 1
+ while True:
+ for j in 1, 2, 5:
+ number = i * j
+ time_taken = self.timeit(number)
+ if callback:
+ callback(number, time_taken)
+ if time_taken >= 0.2:
+ return (number, time_taken)
+ i *= 10
def timeit(stmt="pass", setup="pass", timer=default_timer,
number=default_number, globals=None):