Skip to content

Commit 4913b5b

Browse files
authored
Merge pull request #15 from ddelange/use-monotonic
Use time.monotonic()
2 parents 23963fc + 900f6e0 commit 4913b5b

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

autotime/__init__.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from __future__ import print_function
22

3-
import time
3+
try:
4+
from time import monotonic
5+
except ImportError:
6+
from monotonic import monotonic
47

58
from IPython.core.magics.execution import _format_time as format_delta
69

@@ -13,26 +16,21 @@ class LineWatcher(object):
1316
-----
1417
* Register the `start` and `stop` methods with the IPython events API.
1518
"""
16-
17-
def __init__(self):
18-
self.start_time = 0.0
19+
__slots__ = ['start_time']
1920

2021
def start(self):
21-
self.start_time = time.time()
22+
self.start_time = monotonic()
2223

2324
def stop(self):
24-
stop_time = time.time()
25-
26-
if self.start_time:
27-
diff = stop_time - self.start_time
28-
assert diff >= 0
29-
print('time: {}'.format(format_delta(diff)))
25+
delta = monotonic() - self.start_time
26+
print(u'time: {}'.format(format_delta(delta)))
3027

3128

3229
timer = LineWatcher()
3330

3431

3532
def load_ipython_extension(ip):
33+
timer.start()
3634
ip.events.register('pre_run_cell', timer.start)
3735
ip.events.register('post_run_cell', timer.stop)
3836

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
description='Time everything in IPython',
1818
long_description=long_description,
1919
license='Apache',
20-
install_requires=['ipython'],
20+
install_requires=['ipython', 'monotonic ; python_version < "3.3"'],
2121
classifiers=[
2222
'Environment :: Console',
2323
'Intended Audience :: Developers',

0 commit comments

Comments
 (0)