-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstop_watch.py
101 lines (81 loc) · 2.34 KB
/
stop_watch.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from time import time, gmtime, strftime
import sys
"""
This is a debugging tool for measuring time taken by certain operations
For usage, wrap any piece of code into:
with StopWatch("Any label"):
do
some
code
It summarizes the parts of the code which were under a given label.
In the end, call print_times() for printing how much time
the operations have taken.
"""
_d = dict()
_l = list()
_stack = ()
class StopWatch:
def __init__(self, name):
self.name = name
def __enter__(self):
global _stack
self.ori_stack = _stack
my_id = _stack+(self.name,)
_stack = my_id
global _d, _l
self.last_time = time()
if my_id not in _d:
_d[my_id] = [0.0, 0]
_l.append(my_id)
self.el = _d[my_id]
def __exit__(self, *exception_data):
duration = time() - self.last_time
self.el[0] += duration
self.el[1] += 1
global _stack
_stack = self.ori_stack
def seconds_to_readable(secs):
ori_secs = secs
secs = int(secs)
minutes = secs // 60
secs %= 60
if not minutes: return "{:.3} sec".format(ori_secs)
hours = minutes // 60
minutes %= 60
if not hours: return "{:02}:{:02}".format(minutes,secs)
days = hours // 24
hours %= 24
result = ["{:02}:{:02}:{:02}".format(hours,minutes,secs)]
weeks = days // 7
days %= 7
if days > 0:
days_str = "{} day".format(days)
if days > 1: days_str += "s"
result.append(days_str)
if weeks > 0:
weeks_str = "{} week".format(weeks)
if weeks > 1: weeks_str += "s"
result.append(weeks_str)
result.reverse()
return ", ".join(result)
def print_times():
if not _l: return
scopes = {() : []}
for scope in _l:
scopes[scope[:-1]].append(scope[-1])
scopes[scope] = []
scope_list = []
def process_scope(scope):
if len(scope) > 0: scope_list.append(scope)
for next_name in scopes[scope]:
process_scope(scope+(next_name,))
process_scope(())
print("Time performance")
for mode in scope_list:
secs, enters = _d[mode]
print("{}{}: {} = {} * {}".format(
" "*len(mode), mode[-1],
seconds_to_readable(secs),
enters, secs / enters,
))
sys.stdout.flush()