forked from python-hydro/pyro2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.py
More file actions
161 lines (113 loc) · 3.34 KB
/
Copy pathprofile.py
File metadata and controls
161 lines (113 loc) · 3.34 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""
A very simple profiling class. Define some timers and methods
to start and stop them. A TimerCollection holds a group of
timers. Nesting of timers is tracked so we can pretty print
the profiling information.
tc = TimerCollection()
a = tc.timer('my timer')
This will add 'my timer' to the list of Timers managed by
the TimerCollection. Subsequent calls to timer() will
return the same Timer object.
To start the timer:
a.begin()
... and to end it,
a.end()
For best results, the block of code timed should be large
enough to offset the overhead of the timer class method
calls.
tc.report() prints out a summary of the timing.
Warning: At present, no enforcement is done to ensure proper
nesting.
"""
from __future__ import print_function
import time
class TimerCollection:
def __init__(self):
"""
Initialize the collection of timers
"""
self.timers = []
def timer(self, name):
"""
Create a timer with the given name. If one with that name already
exists, then we return that timer.
Parameters
----------
name : str
Name of the timer
Returns
-------
out : Timer object
A timer object corresponding to the name.
"""
# check if any existing timer has this name, if so, return that
# object
for t in self.timers:
if t.name == name:
return t
break
# if we're here, we didn't find one, so create a new one
# find out how nested we are (the stack count), for pretty printing
stack_count = 0
for t in self.timers:
if t.is_running: stack_count += 1
t_new = Timer(name, stack_count=stack_count)
self.timers.append(t_new)
return t_new
def report(self):
"""
Generate a timing summary report
"""
spacing = ' '
for t in self.timers:
print(t.stack_count*spacing + t.name + ': ', t.elapsed_time)
class Timer:
def __init__ (self, name, stack_count=0):
"""
Initialize a timer with the given name.
Parameters
----------
name : str
The name of the timer
stack_count : int, optional
The depth of the timer (i.e. how many timers is this nested
in). This is used for printing purposes.
"""
self.name = name
self.stack_count = stack_count
self.is_running = False
self.start_time = 0
self.elapsed_time = 0
def begin(self):
"""
Start timing
"""
self.start_time = time.time()
self.is_running = True
def end(self):
"""
Stop timing. This does not destroy the timer, it simply
stops it from counting time.
"""
elapsed_time = time.time() - self.start_time
self.elapsed_time += elapsed_time
self.is_running = False
if __name__ == "__main__":
tc = TimerCollection()
a = tc.timer('a')
a.begin()
time.sleep(10.)
a.end()
b = tc.timer('b')
b.begin()
time.sleep(5.)
c = tc.timer('c')
c.begin()
time.sleep(10.)
c.end()
c = tc.timer('c')
c.begin()
time.sleep(10.)
c.end()
b.end()
tc.report()