update benchmark.py
This commit is contained in:
parent
c9d76f0f54
commit
9f00af2e36
1 changed files with 32 additions and 11 deletions
|
|
@ -1,11 +1,21 @@
|
|||
import time
|
||||
import utime
|
||||
import math
|
||||
import ulab
|
||||
import ulab.numerical
|
||||
|
||||
def timeit(f, *args, **kwargs):
|
||||
func_name = str(f).split(' ')[1]
|
||||
def new_func(*args, **kwargs):
|
||||
t = utime.ticks_us()
|
||||
result = f(*args, **kwargs)
|
||||
print('execution time: ', utime.ticks_diff(utime.ticks_us(), t), ' us')
|
||||
return result
|
||||
return new_func
|
||||
|
||||
def mean(values):
|
||||
return sum(values) / len(values)
|
||||
|
||||
@timeit
|
||||
def normalized_rms(values):
|
||||
minbuf = int(mean(values))
|
||||
samples_sum = sum(
|
||||
|
|
@ -15,6 +25,7 @@ def normalized_rms(values):
|
|||
|
||||
return math.sqrt(samples_sum / len(values))
|
||||
|
||||
@timeit
|
||||
def normalized_rms_ulab(values):
|
||||
minbuf = ulab.numerical.mean(values)
|
||||
values = values - minbuf
|
||||
|
|
@ -22,19 +33,29 @@ def normalized_rms_ulab(values):
|
|||
return math.sqrt(samples_sum / len(values))
|
||||
|
||||
|
||||
@timeit
|
||||
def normalized_std_ulab(values):
|
||||
return ulab.numerical.std(values)
|
||||
|
||||
@timeit
|
||||
def normalized_std_ulab_iterable(values):
|
||||
return ulab.numerical.std(values)
|
||||
|
||||
# Instead of using sensor data, we generate some data
|
||||
# The amplitude is 5000 so the rms should be around 5000/1.414 = 3536
|
||||
nums_list = [int(8000 + math.sin(i) * 5000) for i in range(100)]
|
||||
nums_array = ulab.array(nums_list)
|
||||
|
||||
def timeit(s, f, n=100):
|
||||
t0 = time.monotonic_ns()
|
||||
for _ in range(n):
|
||||
x = f()
|
||||
t1 = time.monotonic_ns()
|
||||
r = (t1 - t0) * 1e-6 / n
|
||||
print("%-20s : %8.3fms [result=%f]" % (s, r, x))
|
||||
|
||||
print("Computing the RMS value of 100 numbers")
|
||||
timeit("traditional", lambda: normalized_rms(nums_list))
|
||||
timeit("ulab", lambda: normalized_rms_ulab(nums_array))
|
||||
|
||||
print('in python')
|
||||
normalized_rms(nums_list)
|
||||
|
||||
print('\nin ulab, with some implementation in python')
|
||||
normalized_rms_ulab(nums_array)
|
||||
|
||||
print('\nin ulab only, with ndarray')
|
||||
normalized_std_ulab(nums_array)
|
||||
|
||||
print('\nin ulab only, with list')
|
||||
normalized_std_ulab_iterable(nums_list)
|
||||
|
|
|
|||
Loading…
Reference in a new issue