-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.py
More file actions
33 lines (24 loc) · 858 Bytes
/
Timer.py
File metadata and controls
33 lines (24 loc) · 858 Bytes
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
from contextlib import contextmanager
from time import time
class Timer:
def __init__(self, logger=None, format_str='{:.3f}[s]', prefix=None, suffix=None, sep=' '):
if prefix: format_str = str(prefix) + sep + format_str
if suffix: format_str = format_str + sep + str(suffix)
self.format_str = format_str
self.logger = logger
self.start = None
self.end = None
@property
def duration(self):
if self.end is None:
return 0
return self.end - self.start
def __enter__(self):
self.start = time()
def __exit__(self, exc_type, exc_val, exc_tb):
self.end = time()
out_str = self.format_str.format(self.duration)
if self.logger:
self.logger.info(out_str)
else:
print(out_str)