Skip to content

Commit aaff82a

Browse files
committed
tests: Add framework for comparative benchmarking.
Motivation is optimizing handling of various constructs as well as understanding which constructs are more efficient in MicroPython. More info: http://forum.micropython.org/viewtopic.php?f=3&t=77 Results are wildly unexpected. For example, "optimization" of range iteration into while loop makes it twice as slow. Generally, the more bytecodes, the slower the code.
1 parent 22a0d67 commit aaff82a

11 files changed

+180
-0
lines changed

tests/bench/bench.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import time
2+
3+
4+
ITERS = 20000000
5+
6+
def run(f):
7+
t = time.time()
8+
f(ITERS)
9+
t = time.time() - t
10+
print(t)

tests/bench/loop_count-1-range.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import bench
2+
3+
def test(num):
4+
for i in range(num):
5+
pass
6+
7+
bench.run(test)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import bench
2+
3+
def test(num):
4+
for i in iter(range(num)):
5+
pass
6+
7+
bench.run(test)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import bench
2+
3+
def test(num):
4+
i = 0
5+
while i < num:
6+
i += 1
7+
8+
bench.run(test)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import bench
2+
3+
def test(num):
4+
while num > 0:
5+
num -= 1
6+
7+
bench.run(test)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import bench
2+
3+
def test(num):
4+
while num != 0:
5+
num -= 1
6+
7+
bench.run(test)

tests/bench/var-1-constant.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import bench
2+
3+
def test(num):
4+
i = 0
5+
while i < 20000000:
6+
i += 1
7+
8+
bench.run(test)

tests/bench/var-2-global.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import bench
2+
3+
ITERS = 20000000
4+
5+
def test(num):
6+
i = 0
7+
while i < ITERS:
8+
i += 1
9+
10+
bench.run(test)

tests/bench/var-3-local.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import bench
2+
3+
4+
def test(num):
5+
ITERS = 20000000
6+
i = 0
7+
while i < ITERS:
8+
i += 1
9+
10+
bench.run(test)

tests/bench/var-4-arg.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import bench
2+
3+
4+
def test(num):
5+
i = 0
6+
while i < num:
7+
i += 1
8+
9+
bench.run(lambda n:test(20000000))

0 commit comments

Comments
 (0)