Skip to content

Commit 11188ad

Browse files
authored
removed micropython-specific code
The update to this PR removes `micropython`-specific code. Tested on an adafruit feather board with the following results: ``` Computing the RMS value of 100 numbers traditional : 5.472ms [result=3535.843611] ulab, with ndarray, some implementation in python : 0.500ms [result=3535.853624] ulab only, with list : 0.655ms [result=3535.854340] ulab only, with ndarray : 0.139ms [result=3535.854340] ``` The result of the second run might be misleading (seems to be faster than the third), because it does not contain the costs of the list-to-ndarray conversion (since that happens earlier, outside of time measurement).
1 parent 9f00af2 commit 11188ad

File tree

1 file changed

+15
-31
lines changed

1 file changed

+15
-31
lines changed

ulab_Crunch_Numbers_Fast/benchmark.py

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
1-
import utime
1+
import time
22
import math
33
import ulab
44
import ulab.numerical
55

6-
def timeit(f, *args, **kwargs):
7-
func_name = str(f).split(' ')[1]
8-
def new_func(*args, **kwargs):
9-
t = utime.ticks_us()
10-
result = f(*args, **kwargs)
11-
print('execution time: ', utime.ticks_diff(utime.ticks_us(), t), ' us')
12-
return result
13-
return new_func
14-
156
def mean(values):
167
return sum(values) / len(values)
178

18-
@timeit
199
def normalized_rms(values):
2010
minbuf = int(mean(values))
2111
samples_sum = sum(
@@ -25,37 +15,31 @@ def normalized_rms(values):
2515

2616
return math.sqrt(samples_sum / len(values))
2717

28-
@timeit
2918
def normalized_rms_ulab(values):
19+
# this function works with ndarrays only
3020
minbuf = ulab.numerical.mean(values)
3121
values = values - minbuf
3222
samples_sum = ulab.numerical.sum(values * values)
3323
return math.sqrt(samples_sum / len(values))
3424

35-
36-
@timeit
3725
def normalized_std_ulab(values):
3826
return ulab.numerical.std(values)
39-
40-
@timeit
41-
def normalized_std_ulab_iterable(values):
42-
return ulab.numerical.std(values)
43-
27+
4428
# Instead of using sensor data, we generate some data
4529
# The amplitude is 5000 so the rms should be around 5000/1.414 = 3536
4630
nums_list = [int(8000 + math.sin(i) * 5000) for i in range(100)]
4731
nums_array = ulab.array(nums_list)
4832

49-
print("Computing the RMS value of 100 numbers")
50-
51-
print('in python')
52-
normalized_rms(nums_list)
53-
54-
print('\nin ulab, with some implementation in python')
55-
normalized_rms_ulab(nums_array)
33+
def timeit(s, f, n=100):
34+
t0 = time.monotonic_ns()
35+
for _ in range(n):
36+
x = f()
37+
t1 = time.monotonic_ns()
38+
r = (t1 - t0) * 1e-6 / n
39+
print("%-20s : %8.3fms [result=%f]" % (s, r, x))
5640

57-
print('\nin ulab only, with ndarray')
58-
normalized_std_ulab(nums_array)
59-
60-
print('\nin ulab only, with list')
61-
normalized_std_ulab_iterable(nums_list)
41+
print("Computing the RMS value of 100 numbers")
42+
timeit("traditional", lambda: normalized_rms(nums_list))
43+
timeit("ulab, with ndarray, some implementation in python", lambda: normalized_rms_ulab(nums_array))
44+
timeit("ulab only, with list", lambda: normalized_std_ulab(nums_list))
45+
timeit("ulab only, with ndarray", lambda: normalized_std_ulab(nums_array))

0 commit comments

Comments
 (0)