kopia lustrzana https://github.com/peterhinch/micropython-samples
Timed function's handling of new ticks_diff changed
rodzic
6a5fd86bdb
commit
eda4e2bbd9
|
@ -1,23 +1,15 @@
|
||||||
# Time a function call by means of a decorator
|
# Time a function call by means of a decorator
|
||||||
|
|
||||||
|
# On or shortly beore 1st November 2016 the semantics of utime.ticks_diff changed. If using
|
||||||
|
# older firmware please use the second example below
|
||||||
|
|
||||||
import utime
|
import utime
|
||||||
def tdiff():
|
|
||||||
new_semantics = utime.ticks_diff(2, 1) == 1
|
|
||||||
def func(old, new):
|
|
||||||
nonlocal new_semantics
|
|
||||||
if new_semantics:
|
|
||||||
return utime.ticks_diff(new, old)
|
|
||||||
return utime.ticks_diff(old, new)
|
|
||||||
return func
|
|
||||||
|
|
||||||
ticksdiff = tdiff()
|
|
||||||
|
|
||||||
def timed_function(f, *args, **kwargs):
|
def timed_function(f, *args, **kwargs):
|
||||||
myname = str(f).split(' ')[1]
|
myname = str(f).split(' ')[1]
|
||||||
def new_func(*args, **kwargs):
|
def new_func(*args, **kwargs):
|
||||||
t = utime.ticks_us()
|
t = utime.ticks_us()
|
||||||
result = f(*args, **kwargs)
|
result = f(*args, **kwargs)
|
||||||
delta = ticksdiff(t, utime.ticks_us())
|
delta = utime.ticks_diff(utime.ticks_us(), t) # Argument order new, old
|
||||||
print('Function {} Time = {:6.3f}ms'.format(myname, delta/1000))
|
print('Function {} Time = {:6.3f}ms'.format(myname, delta/1000))
|
||||||
return result
|
return result
|
||||||
return new_func
|
return new_func
|
||||||
|
@ -26,3 +18,15 @@ def timed_function(f, *args, **kwargs):
|
||||||
def test():
|
def test():
|
||||||
utime.sleep_us(10000)
|
utime.sleep_us(10000)
|
||||||
|
|
||||||
|
# Version for use with older firmware
|
||||||
|
def timed_function(f, *args, **kwargs):
|
||||||
|
myname = str(f).split(' ')[1]
|
||||||
|
def new_func(*args, **kwargs):
|
||||||
|
t = utime.ticks_us()
|
||||||
|
result = f(*args, **kwargs)
|
||||||
|
delta = utime.ticks_diff(t, utime.ticks_us()) # Argument order old, new
|
||||||
|
print('Function {} Time = {:6.3f}ms'.format(myname, delta/1000))
|
||||||
|
return result
|
||||||
|
return new_func
|
||||||
|
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue