-
Notifications
You must be signed in to change notification settings - Fork 3
/
timingRoutines.py
91 lines (73 loc) · 2.11 KB
/
timingRoutines.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 24 11:52:27 2022
@author: seoxubuntu
"""
import time
class Timer:
def __init__(self):
self.t = []
self.labels = []
def reset(self):
self.t.clear()
self.labels.clear()
def start(self):
"""
Convenience function to start the timer (which by definition should have no label).
Automatically calls reset() before beginning.
"""
self.reset()
self.evt()
def evt(self, label: str = ""):
"""
Adds an event i.e. records the current time using perf_counter().
Also adds a label if desired, describing the time elapsed since the last event.
Parameters
----------
label : str, optional
The default is "".
"""
self.t.append(time.perf_counter())
self.labels.append(label)
def rpt(self, showSteps: bool = True):
"""
Reports times elapsed between events.
Returns
-------
total : float
Total duration of the timer, in seconds.
"""
if showSteps:
for i in range(1, len(self.t)):
print(
"%d->%d : %fs. %s"
% (i - 1, i, self.t[i] - self.t[i - 1], self.labels[i])
)
# Always print total
total = self.t[-1] - self.t[0]
print("Total: %fs." % (total))
return total
def end(self, label: str = "", showSteps: bool = True):
"""
Convenience function to add an event and report immediately.
Returns
-------
total : float
Total duration of the timer, in seconds.
"""
self.evt(label)
total = self.rpt(showSteps=showSteps)
return total
if __name__ == "__main__":
import numpy as np
length = int(10e6)
timer = Timer()
timer.start()
data = np.random.rand(length) + 1j * np.random.rand(length)
timer.evt("Rand")
f = np.fft.fft(data)
timer.end("FFT")
timer.reset()
timer.start()
timer.end("Timer overhead")