forked from wendlers/micropython-mfrc522
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtimer.py
37 lines (34 loc) · 949 Bytes
/
timer.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
from utime import ticks_ms, ticks_diff
log = True
timers = {}
depth = 0
whitespace = " "
def startTimer(name):
timers[name] = ticks_ms()
indent = whitespace * depth
if log:
print("{}{} started".format(indent, name))
def endTimer(name):
if name in timers:
startms = timers[name]
sincems = ticks_diff(ticks_ms(), startms)
indent = whitespace * depth
if log:
print("{}{} took {}ms".format(indent, name, sincems))
del timers[name]
else:
print("NO SUCH TIMER '{}' !".format(name))
def timeit(timerName):
def factory(method):
def timed(*args, **kw):
global depth
try:
startTimer(timerName)
depth += 1
result = method(*args, **kw)
depth -= 1
finally:
endTimer(timerName)
return result
return timed
return factory