-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.py
58 lines (47 loc) · 1.5 KB
/
timer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from __future__ import division, print_function
from timeit import default_timer
class Timer(object):
""" Utility class used for monitoring code execution time. This class
offers two usage patterns - as a context manager or as a regular object.
"""
def __init__(self, start=False):
""" Initialize the Timer and optionally start it.
Parameters
----------
start : bool
Flag indicating whether to start the timer.
"""
self.reset(start=start)
def __enter__(self):
self.reset(start=True)
return self
def __exit__(self, exc_type, exc_value, traceback):
self.stop()
def reset(self, start=False):
""" Reset the Timer and the optionally start it.
Parameters
----------
start : bool
Flag indicating whether to start the timer.
"""
self._start, self._stop = 0, 0
self._stopped = True
if start:
self.start()
def start(self):
""" Start the Timer."""
self._start = default_timer()
self._stopped = False
def stop(self):
""" Stop the Timer."""
self._stop = default_timer()
self._stopped = True
@property
def elapsed(self):
""" Property for getting the elapsed time. Stops the Timer if it has
not been stopped explicitly.
"""
if not self._stopped:
self.stop()
return self.elapsed
return self._stop - self._start