Skip to content

Commit 0a6d08b

Browse files
committed
fix occasional test failures due to limited reliability of the sleep() function
1 parent 0ec1fc3 commit 0a6d08b

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

pyNN/utility/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
init_logging() - convenience function for setting up logging to file and
1010
to the screen.
1111
12-
Timer - a convenience wrapper around the time.time() function from the
12+
Timer - a convenience wrapper around the time.perf_counter() function from the
1313
standard library.
1414
1515
:copyright: Copyright 2006-2021 by the PyNN team, see AUTHORS.
@@ -240,7 +240,7 @@ def __init__(self):
240240

241241
def start(self):
242242
"""Start/restart timing."""
243-
self._start_time = time.time()
243+
self._start_time = time.perf_counter()
244244
self._last_check = self._start_time
245245

246246
def elapsed_time(self, format=None):
@@ -255,7 +255,7 @@ def elapsed_time(self, format=None):
255255
>>> timer.elapsed_time(format='long')
256256
16 minutes, 27 seconds
257257
"""
258-
current_time = time.time()
258+
current_time = time.perf_counter()
259259
elapsed_time = current_time - self._start_time
260260
if format == 'long':
261261
elapsed_time = Timer.time_in_words(elapsed_time)
@@ -278,7 +278,7 @@ def diff(self, format=None): # I think delta() would be a better name for this
278278
If called with ``format='long'``, return a text representation of the
279279
time.
280280
"""
281-
current_time = time.time()
281+
current_time = time.perf_counter()
282282
time_since_last_check = current_time - self._last_check
283283
self._last_check = current_time
284284
if format == 'long':

test/unittests/test_utility_functions.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,13 @@ def test_timer(self):
4545
def test_diff(self):
4646
timer = utility.Timer()
4747
time.sleep(0.1)
48-
self.assertAlmostEqual(timer.diff(), 0.1, places=2)
48+
self.assertAlmostEqual(timer.diff(), 0.1, places=1)
4949
time.sleep(0.2)
5050
self.assertAlmostEqual(timer.diff(), 0.2, places=1)
51-
self.assertAlmostEqual(timer.elapsed_time(), 0.3, places=2)
51+
self.assertAlmostEqual(timer.elapsed_time(), 0.3, places=1)
52+
# we only check to 1 decimal place because with sleep():
53+
# "the suspension time may be longer than requested by an arbitrary amount"
54+
# "because of the scheduling of other activity in the system"
5255

5356

5457
class ProgressBarTest(unittest.TestCase):

0 commit comments

Comments
 (0)