diff options
-rw-r--r-- | Doc/library/timeit.rst | 23 | ||||
-rw-r--r-- | Lib/test/test_timeit.py | 63 | ||||
-rw-r--r-- | Lib/timeit.py | 25 | ||||
-rw-r--r-- | Misc/NEWS | 3 |
4 files changed, 65 insertions, 49 deletions
diff --git a/Doc/library/timeit.rst b/Doc/library/timeit.rst index d4ddd64..4065808 100644 --- a/Doc/library/timeit.rst +++ b/Doc/library/timeit.rst @@ -28,11 +28,11 @@ can be used to compare three different expressions: .. code-block:: sh $ python3 -m timeit '"-".join(str(n) for n in range(100))' - 10000 loops, best of 3: 30.2 usec per loop + 10000 loops, best of 5: 30.2 usec per loop $ python3 -m timeit '"-".join([str(n) for n in range(100)])' - 10000 loops, best of 3: 27.5 usec per loop + 10000 loops, best of 5: 27.5 usec per loop $ python3 -m timeit '"-".join(map(str, range(100)))' - 10000 loops, best of 3: 23.2 usec per loop + 10000 loops, best of 5: 23.2 usec per loop This can be achieved from the :ref:`python-interface` with:: @@ -141,9 +141,8 @@ The module defines three convenience functions and a public class: This is a convenience function that calls :meth:`.timeit` repeatedly so that the total time >= 0.2 second, returning the eventual (number of loops, time taken for that number of loops). It calls - :meth:`.timeit` 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. + :meth:`.timeit` with increasing numbers from the sequence 1, 2, 5, + 10, 20, 50, ... until the time taken is at least 0.2 second. If *callback* is given and is not ``None``, it will be called after each trial with two arguments: ``callback(number, time_taken)``. @@ -268,9 +267,9 @@ It is possible to provide a setup statement that is executed only once at the be .. code-block:: sh $ python -m timeit -s 'text = "sample string"; char = "g"' 'char in text' - 10000000 loops, best of 3: 0.0877 usec per loop + 5000000 loops, best of 5: 0.0877 usec per loop $ python -m timeit -s 'text = "sample string"; char = "g"' 'text.find(char)' - 1000000 loops, best of 3: 0.342 usec per loop + 1000000 loops, best of 5: 0.342 usec per loop :: @@ -297,14 +296,14 @@ to test for missing and present object attributes: .. code-block:: sh $ python -m timeit 'try:' ' str.__bool__' 'except AttributeError:' ' pass' - 100000 loops, best of 3: 15.7 usec per loop + 20000 loops, best of 5: 15.7 usec per loop $ python -m timeit 'if hasattr(str, "__bool__"): pass' - 100000 loops, best of 3: 4.26 usec per loop + 50000 loops, best of 5: 4.26 usec per loop $ python -m timeit 'try:' ' int.__bool__' 'except AttributeError:' ' pass' - 1000000 loops, best of 3: 1.43 usec per loop + 200000 loops, best of 5: 1.43 usec per loop $ python -m timeit 'if hasattr(int, "__bool__"): pass' - 100000 loops, best of 3: 2.23 usec per loop + 100000 loops, best of 5: 2.23 usec per loop :: 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): @@ -97,6 +97,9 @@ Core and Builtins Library ------- +- Issue #28469: timeit now uses the sequence 1, 2, 5, 10, 20, 50,... instead + of 1, 10, 100,... for autoranging. + - Issue #28115: Command-line interface of the zipfile module now uses argparse. Added support of long options. |