From 3b78ef2a402af30a4887fb3d8d282e985ef6ae58 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 18 Oct 2016 17:06:56 +0200 Subject: 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. --- Lib/test/test_timeit.py | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) (limited to 'Lib/test/test_timeit.py') diff --git a/Lib/test/test_timeit.py b/Lib/test/test_timeit.py index 1a95e2979c..3b929923e4 100644 --- a/Lib/test/test_timeit.py +++ b/Lib/test/test_timeit.py @@ -226,7 +226,7 @@ class TestTimeit(unittest.TestCase): t.print_exc(s) self.assert_exc_string(s.getvalue(), 'ZeroDivisionError') - MAIN_DEFAULT_OUTPUT = "10 loops, best of 3: 1 sec per loop\n" + MAIN_DEFAULT_OUTPUT = "1 loop, best of 3: 1 sec per loop\n" def run_main(self, seconds_per_increment=1.0, switches=None, timer=None): if timer is None: @@ -252,7 +252,7 @@ class TestTimeit(unittest.TestCase): def test_main_seconds(self): s = self.run_main(seconds_per_increment=5.5) - self.assertEqual(s, "10 loops, best of 3: 5.5 sec per loop\n") + self.assertEqual(s, "1 loop, best of 3: 5.5 sec per loop\n") def test_main_milliseconds(self): s = self.run_main(seconds_per_increment=0.0055) @@ -280,11 +280,11 @@ class TestTimeit(unittest.TestCase): def test_main_fixed_reps(self): s = self.run_main(seconds_per_increment=60.0, switches=['-r9']) - self.assertEqual(s, "10 loops, best of 9: 60 sec per loop\n") + self.assertEqual(s, "1 loop, best of 9: 60 sec per loop\n") def test_main_negative_reps(self): s = self.run_main(seconds_per_increment=60.0, switches=['-r-5']) - self.assertEqual(s, "10 loops, best of 1: 60 sec per loop\n") + self.assertEqual(s, "1 loop, best of 1: 60 sec per loop\n") @unittest.skipIf(sys.flags.optimize >= 2, "need __doc__") def test_main_help(self): @@ -308,14 +308,15 @@ class TestTimeit(unittest.TestCase): def test_main_verbose(self): s = self.run_main(switches=['-v']) self.assertEqual(s, dedent("""\ - 10 loops -> 10 secs - raw times: 10 10 10 - 10 loops, best of 3: 1 sec per loop + 1 loop -> 1 secs + raw times: 1 1 1 + 1 loop, best of 3: 1 sec per loop """)) def test_main_very_verbose(self): s = self.run_main(seconds_per_increment=0.000050, 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 @@ -328,15 +329,15 @@ class TestTimeit(unittest.TestCase): unit_sec = self.run_main(seconds_per_increment=0.002, switches=['-u', 'sec']) self.assertEqual(unit_sec, - "1000 loops, best of 3: 0.002 sec per loop\n") + "100 loops, best of 3: 0.002 sec per loop\n") unit_msec = self.run_main(seconds_per_increment=0.002, switches=['-u', 'msec']) self.assertEqual(unit_msec, - "1000 loops, best of 3: 2 msec per loop\n") + "100 loops, best of 3: 2 msec per loop\n") unit_usec = self.run_main(seconds_per_increment=0.002, switches=['-u', 'usec']) self.assertEqual(unit_usec, - "1000 loops, best of 3: 2e+03 usec per loop\n") + "100 loops, best of 3: 2e+03 usec per loop\n") # Test invalid unit input with captured_stderr() as error_stringio: invalid = self.run_main(seconds_per_increment=0.002, @@ -354,8 +355,8 @@ class TestTimeit(unittest.TestCase): s = self.run_main(switches=['-n1', '1/0']) self.assert_exc_string(error_stringio.getvalue(), 'ZeroDivisionError') - def autorange(self, callback=None): - timer = FakeTimer(seconds_per_increment=0.001) + def autorange(self, seconds_per_increment=0.001, 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) @@ -364,14 +365,20 @@ class TestTimeit(unittest.TestCase): self.assertEqual(num_loops, 1000) self.assertEqual(time_taken, 1.0) + def test_autorange_second(self): + num_loops, time_taken = self.autorange(seconds_per_increment=1.0) + self.assertEqual(num_loops, 1) + self.assertEqual(time_taken, 1.0) + def test_autorange_with_callback(self): def callback(a, b): print("{} {:.3f}".format(a, b)) with captured_stdout() as s: - num_loops, time_taken = self.autorange(callback) + num_loops, time_taken = self.autorange(callback=callback) self.assertEqual(num_loops, 1000) self.assertEqual(time_taken, 1.0) - expected = ('10 0.010\n' + expected = ('1 0.001\n' + '10 0.010\n' '100 0.100\n' '1000 1.000\n') self.assertEqual(s.getvalue(), expected) -- cgit v1.2.1 From b9f2189bd33cf6daeafb8f6ec30f64db1f1f58f0 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 18 Oct 2016 17:13:22 +0200 Subject: timeit: change default repeat to 5, instead of 3 Issue #28240: timeit now repeats the benchmarks 5 times instead of only 3 to make benchmarks more reliable. --- Lib/test/test_timeit.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'Lib/test/test_timeit.py') diff --git a/Lib/test/test_timeit.py b/Lib/test/test_timeit.py index 3b929923e4..7a2be53a08 100644 --- a/Lib/test/test_timeit.py +++ b/Lib/test/test_timeit.py @@ -12,7 +12,7 @@ from test.support import captured_stderr DEFAULT_NUMBER = 1000000 # timeit's default number of repetitions. -DEFAULT_REPEAT = 3 +DEFAULT_REPEAT = 5 # XXX: some tests are commented out that would improve the coverage but take a # long time to run because they test the default number of loops, which is @@ -226,7 +226,7 @@ class TestTimeit(unittest.TestCase): t.print_exc(s) self.assert_exc_string(s.getvalue(), 'ZeroDivisionError') - MAIN_DEFAULT_OUTPUT = "1 loop, best of 3: 1 sec per loop\n" + MAIN_DEFAULT_OUTPUT = "1 loop, best of 5: 1 sec per loop\n" def run_main(self, seconds_per_increment=1.0, switches=None, timer=None): if timer is None: @@ -252,31 +252,31 @@ class TestTimeit(unittest.TestCase): def test_main_seconds(self): s = self.run_main(seconds_per_increment=5.5) - self.assertEqual(s, "1 loop, best of 3: 5.5 sec per loop\n") + self.assertEqual(s, "1 loop, best of 5: 5.5 sec per loop\n") def test_main_milliseconds(self): s = self.run_main(seconds_per_increment=0.0055) - self.assertEqual(s, "100 loops, best of 3: 5.5 msec per loop\n") + self.assertEqual(s, "100 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']) - self.assertEqual(s, "100 loops, best of 3: 2.5 usec per loop\n") + self.assertEqual(s, "100 loops, best of 5: 2.5 usec per loop\n") def test_main_fixed_iters(self): s = self.run_main(seconds_per_increment=2.0, switches=['-n35']) - self.assertEqual(s, "35 loops, best of 3: 2 sec per loop\n") + self.assertEqual(s, "35 loops, best of 5: 2 sec per loop\n") def test_main_setup(self): s = self.run_main(seconds_per_increment=2.0, switches=['-n35', '-s', 'print("CustomSetup")']) - self.assertEqual(s, "CustomSetup\n" * 3 + - "35 loops, best of 3: 2 sec per loop\n") + self.assertEqual(s, "CustomSetup\n" * DEFAULT_REPEAT + + "35 loops, best of 5: 2 sec per loop\n") def test_main_multiple_setups(self): s = self.run_main(seconds_per_increment=2.0, switches=['-n35', '-s', 'a = "CustomSetup"', '-s', 'print(a)']) - self.assertEqual(s, "CustomSetup\n" * 3 + - "35 loops, best of 3: 2 sec per loop\n") + self.assertEqual(s, "CustomSetup\n" * DEFAULT_REPEAT + + "35 loops, best of 5: 2 sec per loop\n") def test_main_fixed_reps(self): s = self.run_main(seconds_per_increment=60.0, switches=['-r9']) @@ -309,8 +309,8 @@ class TestTimeit(unittest.TestCase): s = self.run_main(switches=['-v']) self.assertEqual(s, dedent("""\ 1 loop -> 1 secs - raw times: 1 1 1 - 1 loop, best of 3: 1 sec per loop + raw times: 1 1 1 1 1 + 1 loop, best of 5: 1 sec per loop """)) def test_main_very_verbose(self): @@ -321,23 +321,23 @@ class TestTimeit(unittest.TestCase): 100 loops -> 0.005 secs 1000 loops -> 0.05 secs 10000 loops -> 0.5 secs - raw times: 0.5 0.5 0.5 - 10000 loops, best of 3: 50 usec per loop + raw times: 0.5 0.5 0.5 0.5 0.5 + 10000 loops, best of 5: 50 usec per loop """)) def test_main_with_time_unit(self): unit_sec = self.run_main(seconds_per_increment=0.002, switches=['-u', 'sec']) self.assertEqual(unit_sec, - "100 loops, best of 3: 0.002 sec per loop\n") + "100 loops, best of 5: 0.002 sec per loop\n") unit_msec = self.run_main(seconds_per_increment=0.002, switches=['-u', 'msec']) self.assertEqual(unit_msec, - "100 loops, best of 3: 2 msec per loop\n") + "100 loops, best of 5: 2 msec per loop\n") unit_usec = self.run_main(seconds_per_increment=0.002, switches=['-u', 'usec']) self.assertEqual(unit_usec, - "100 loops, best of 3: 2e+03 usec per loop\n") + "100 loops, best of 5: 2e+03 usec per loop\n") # Test invalid unit input with captured_stderr() as error_stringio: invalid = self.run_main(seconds_per_increment=0.002, -- cgit v1.2.1 From 707a52a156851c7d8f6ebaedb6c98ef52a756186 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 18 Oct 2016 17:18:21 +0200 Subject: timeit: remove --clock and --time options Issue #28240: timeit: remove -c/--clock and -t/--time command line options which were deprecated since Python 3.3. --- Lib/test/test_timeit.py | 12 ------------ 1 file changed, 12 deletions(-) (limited to 'Lib/test/test_timeit.py') diff --git a/Lib/test/test_timeit.py b/Lib/test/test_timeit.py index 7a2be53a08..cf4eec9719 100644 --- a/Lib/test/test_timeit.py +++ b/Lib/test/test_timeit.py @@ -293,18 +293,6 @@ class TestTimeit(unittest.TestCase): # the help text, but since it's there, check for it. self.assertEqual(s, timeit.__doc__ + ' ') - def test_main_using_time(self): - fake_timer = FakeTimer() - s = self.run_main(switches=['-t'], timer=fake_timer) - self.assertEqual(s, self.MAIN_DEFAULT_OUTPUT) - self.assertIs(fake_timer.saved_timer, time.time) - - def test_main_using_clock(self): - fake_timer = FakeTimer() - s = self.run_main(switches=['-c'], timer=fake_timer) - self.assertEqual(s, self.MAIN_DEFAULT_OUTPUT) - self.assertIs(fake_timer.saved_timer, time.clock) - def test_main_verbose(self): s = self.run_main(switches=['-v']) self.assertEqual(s, dedent("""\ -- cgit v1.2.1 From 4dcc8647b23d484724821c14be42aa8a3c82ad7b Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 18 Oct 2016 17:56:42 +0200 Subject: timeit: enhance format of raw timings (in verbose mode) Issue #28240. --- Lib/test/test_timeit.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'Lib/test/test_timeit.py') diff --git a/Lib/test/test_timeit.py b/Lib/test/test_timeit.py index cf4eec9719..b297222ab2 100644 --- a/Lib/test/test_timeit.py +++ b/Lib/test/test_timeit.py @@ -297,7 +297,7 @@ class TestTimeit(unittest.TestCase): s = self.run_main(switches=['-v']) self.assertEqual(s, dedent("""\ 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 """)) @@ -309,7 +309,7 @@ class TestTimeit(unittest.TestCase): 100 loops -> 0.005 secs 1000 loops -> 0.05 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 """)) -- cgit v1.2.1 From 29560e62d585e46739ddf27f630337b1f5dc8198 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 18 Oct 2016 17:42:48 +0200 Subject: timeit: add nsec (nanosecond) unit for format timings Issue #28240. --- Lib/test/test_timeit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Lib/test/test_timeit.py') diff --git a/Lib/test/test_timeit.py b/Lib/test/test_timeit.py index b297222ab2..4e8c1181b2 100644 --- a/Lib/test/test_timeit.py +++ b/Lib/test/test_timeit.py @@ -331,7 +331,7 @@ class TestTimeit(unittest.TestCase): invalid = self.run_main(seconds_per_increment=0.002, switches=['-u', 'parsec']) self.assertEqual(error_stringio.getvalue(), - "Unrecognized unit. Please select usec, msec, or sec.\n") + "Unrecognized unit. Please select nsec, usec, msec, or sec.\n") def test_main_exception(self): with captured_stderr() as error_stringio: -- cgit v1.2.1 From b9d91733a5faaae5acf8729175e70fd8440f546b Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 18 Oct 2016 17:55:18 +0200 Subject: timeit: add newlines to output for readability Issue #28240. --- Lib/test/test_timeit.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'Lib/test/test_timeit.py') diff --git a/Lib/test/test_timeit.py b/Lib/test/test_timeit.py index 4e8c1181b2..a4298b5995 100644 --- a/Lib/test/test_timeit.py +++ b/Lib/test/test_timeit.py @@ -297,7 +297,9 @@ class TestTimeit(unittest.TestCase): s = self.run_main(switches=['-v']) self.assertEqual(s, dedent("""\ 1 loop -> 1 secs + raw times: 1 sec, 1 sec, 1 sec, 1 sec, 1 sec + 1 loop, best of 5: 1 sec per loop """)) @@ -309,7 +311,9 @@ class TestTimeit(unittest.TestCase): 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 """)) -- cgit v1.2.1 From f1796723da117f8ffff0882333777e26fc0acb53 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 23 Oct 2016 15:17:05 +0300 Subject: Issue #28469: timeit now uses the sequence 1, 2, 5, 10, 20, 50,... instead of 1, 10, 100,... for autoranging. --- Lib/test/test_timeit.py | 63 +++++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 25 deletions(-) (limited to 'Lib/test/test_timeit.py') diff --git a/Lib/test/test_timeit.py b/Lib/test/test_timeit.py index a4298b5995..8d4746a36e 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) -- cgit v1.2.1 From 71671fce07c5e1a483970ddcd92895d834bd7e08 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Fri, 16 Dec 2016 20:00:15 +0200 Subject: Remove unused imports. --- Lib/test/test_timeit.py | 1 - 1 file changed, 1 deletion(-) (limited to 'Lib/test/test_timeit.py') diff --git a/Lib/test/test_timeit.py b/Lib/test/test_timeit.py index 8d4746a36e..e02d4a71a9 100644 --- a/Lib/test/test_timeit.py +++ b/Lib/test/test_timeit.py @@ -2,7 +2,6 @@ import timeit import unittest import sys import io -import time from textwrap import dedent from test.support import captured_stdout -- cgit v1.2.1