Skip to content

Commit 747847c

Browse files
committed
bpo-36461: Improve the code after review comments
1 parent c292a30 commit 747847c

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

Doc/library/timeit.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ The module defines three convenience functions and a public class:
146146
Automatically determine how many times to call :meth:`.timeit`.
147147

148148
This is a convenience function that calls :meth:`.timeit` repeatedly
149-
so that the total time is greater than *total_time*, returning the eventual
149+
so that the total time is greater than or equal to *total_time*, returning the eventual
150150
(number of loops, time taken for that number of loops). It calls
151151
:meth:`.timeit` with increasing numbers from the sequence 1, 2, 5,
152152
10, 20, 50, ... until the time taken is at least *total_time*.

Lib/test/test_timeit.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ def test_main_exception_fixed_reps(self):
355355
self.assert_exc_string(error_stringio.getvalue(), 'ZeroDivisionError')
356356

357357
def autorange(self, seconds_per_increment=1/1024, callback=None,
358-
total_time=0.2):
358+
total_time=0.2):
359359
timer = FakeTimer(seconds_per_increment=seconds_per_increment)
360360
t = timeit.Timer(stmt=self.fake_stmt, setup=self.fake_setup, timer=timer)
361361
return t.autorange(callback, total_time)
@@ -382,35 +382,39 @@ def callback(a, b):
382382
num_loops, time_taken = self.autorange(callback=callback)
383383
self.assertEqual(num_loops, 500)
384384
self.assertEqual(time_taken, 500/1024)
385-
expected = ('1 0.001\n'
386-
'2 0.002\n'
387-
'5 0.005\n'
388-
'10 0.010\n'
389-
'20 0.020\n'
390-
'50 0.049\n'
391-
'100 0.098\n'
392-
'200 0.195\n'
393-
'500 0.488\n')
385+
expected = dedent('''\
386+
1 0.001
387+
2 0.002
388+
5 0.005
389+
10 0.010
390+
20 0.020
391+
50 0.049
392+
100 0.098
393+
200 0.195
394+
500 0.488
395+
''')
394396
self.assertEqual(s.getvalue(), expected)
395397

396398
def test_autorange_with_callback_and_total_time(self):
397399
def callback(a, b):
398400
print("{} {:.3f}".format(a, b))
399401
with captured_stdout() as s:
400402
num_loops, time_taken = self.autorange(callback=callback,
401-
total_time=0.6)
403+
total_time=0.6)
402404
self.assertEqual(num_loops, 1000)
403405
self.assertEqual(time_taken, 1000/1024)
404-
expected = ('1 0.001\n'
405-
'2 0.002\n'
406-
'5 0.005\n'
407-
'10 0.010\n'
408-
'20 0.020\n'
409-
'50 0.049\n'
410-
'100 0.098\n'
411-
'200 0.195\n'
412-
'500 0.488\n'
413-
'1000 0.977\n')
406+
expected = dedent('''\
407+
1 0.001
408+
2 0.002
409+
5 0.005
410+
10 0.010
411+
20 0.020
412+
50 0.049
413+
100 0.098
414+
200 0.195
415+
500 0.488
416+
1000 0.977
417+
''')
414418
self.assertEqual(s.getvalue(), expected)
415419

416420

Lib/timeit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def repeat(self, repeat=default_repeat, number=default_number):
207207

208208
def autorange(self, callback=None, total_time=0.2):
209209
"""Return the number of loops and time taken so that total time
210-
is greater than *total_time*.
210+
is greater than or equal to *total_time*.
211211
212212
Calls the timeit method with increasing numbers from the sequence
213213
1, 2, 5, 10, 20, 50, ... until the time taken is at least 0.2

0 commit comments

Comments
 (0)