-
Notifications
You must be signed in to change notification settings - Fork 95
/
timed_func.py
38 lines (31 loc) · 1.05 KB
/
timed_func.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
# Time a function call by means of a decorator
import utime
# @timed_function
# Print time taken by a function call
def timed_function(f, *args, **kwargs):
def new_func(*args, **kwargs):
t = utime.ticks_us()
result = f(*args, **kwargs)
delta = utime.ticks_diff(utime.ticks_us(), t)
print('Function {} Time = {:6.3f}ms'.format(f.__name__, delta/1000))
return result
return new_func
@timed_function
def test():
utime.sleep_us(10000)
# @time_acc_function
# applied to a function causes it to print the number of times it was called
# with the accumulated time used.
def time_acc_function(f, *args, **kwargs):
ncalls = 0
ttime = 0.0
def new_func(*args, **kwargs):
nonlocal ncalls, ttime
t = utime.ticks_us()
result = f(*args, **kwargs)
delta = utime.ticks_diff(utime.ticks_us(), t)
ncalls += 1
ttime += delta
print('Function: {} Call count = {} Total time = {:6.3f}ms'.format(f.__name__, ncalls, ttime/1000))
return result
return new_func