-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofiling.py
More file actions
26 lines (21 loc) · 816 Bytes
/
Copy pathprofiling.py
File metadata and controls
26 lines (21 loc) · 816 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
import datetime
s_print_results = True
s_profiling_stack = []
class Timer:
def __init__(self, description):
assert isinstance(description, str)
if s_print_results:
print(f"{'| ' * len(s_profiling_stack)}{description} started")
self.description = description
self.start_time = datetime.datetime.now()
s_profiling_stack.append(id(self))
def done(self):
assert s_profiling_stack[-1] == id(self)
s_profiling_stack.pop()
now = datetime.datetime.now()
duration = now - self.start_time
seconds = float(duration.seconds) + (duration.microseconds / 1_000_000.0)
if s_print_results:
print(
f"{'| ' * len(s_profiling_stack)}{self.description} done, {seconds} seconds"
)