micropython: add micropython component

This commit is contained in:
KY-zhang-X
2022-09-29 12:10:37 +08:00
parent 1514f1cb9b
commit dd76146324
2679 changed files with 354110 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
# Array operation
# Type: list, inplace operation using for. What's good about this
# method is that it doesn't require any extra memory allocation.
import bench
def test(num):
for i in iter(range(num // 10000)):
arr = [0] * 1000
for i in range(len(arr)):
arr[i] += 1
bench.run(test)

View File

@@ -0,0 +1,14 @@
# Array operation
# Type: list, map() call. This method requires allocation of
# the same amount of memory as original array (to hold result
# array). On the other hand, input array stays intact.
import bench
def test(num):
for i in iter(range(num // 10000)):
arr = [0] * 1000
arr2 = list(map(lambda x: x + 1, arr))
bench.run(test)

View File

@@ -0,0 +1,14 @@
# Array operation
# Type: bytearray, inplace operation using for. What's good about this
# method is that it doesn't require any extra memory allocation.
import bench
def test(num):
for i in iter(range(num // 10000)):
arr = bytearray(b"\0" * 1000)
for i in range(len(arr)):
arr[i] += 1
bench.run(test)

View File

@@ -0,0 +1,14 @@
# Array operation
# Type: list, map() call. This method requires allocation of
# the same amount of memory as original array (to hold result
# array). On the other hand, input array stays intact.
import bench
def test(num):
for i in iter(range(num // 10000)):
arr = bytearray(b"\0" * 1000)
arr2 = bytearray(map(lambda x: x + 1, arr))
bench.run(test)

View File

@@ -0,0 +1,11 @@
import time
ITERS = 20000000
def run(f):
t = time.time()
f(ITERS)
t = time.time() - t
print(t)

View File

@@ -0,0 +1,9 @@
import bench
def test(num):
for i in iter(range(num // 1000)):
bytes(10000)
bench.run(test)

View File

@@ -0,0 +1,9 @@
import bench
def test(num):
for i in iter(range(num // 1000)):
b"\0" * 10000
bench.run(test)

View File

@@ -0,0 +1,13 @@
# Doing some operation on bytearray
# Inplace - the most memory efficient way
import bench
def test(num):
for i in iter(range(num // 10000)):
ba = bytearray(b"\0" * 1000)
for i in range(len(ba)):
ba[i] += 1
bench.run(test)

View File

@@ -0,0 +1,14 @@
# Doing some operation on bytearray
# Pretty weird way - map bytearray thru function, but make sure that
# function return bytes of size 1, then join them together. Surely,
# this is slowest way to do it.
import bench
def test(num):
for i in iter(range(num // 10000)):
ba = bytearray(b"\0" * 1000)
ba2 = b"".join(map(lambda x: bytes([x + 1]), ba))
bench.run(test)

View File

@@ -0,0 +1,12 @@
# Doing some operation on bytearray
# No joins, but still map().
import bench
def test(num):
for i in iter(range(num // 10000)):
ba = bytearray(b"\0" * 1000)
ba2 = bytearray(map(lambda x: x + 1, ba))
bench.run(test)

View File

@@ -0,0 +1,10 @@
import bench
def test(num):
for i in iter(range(num // 10000)):
l = [0] * 1000
l2 = list(l)
bench.run(test)

View File

@@ -0,0 +1,10 @@
import bench
def test(num):
for i in iter(range(num // 10000)):
l = [0] * 1000
l2 = list(map(lambda x: x, l))
bench.run(test)

View File

@@ -0,0 +1,10 @@
import bench
def test(num):
for i in iter(range(num // 10000)):
l = [0] * 1000
l2 = tuple(l)
bench.run(test)

View File

@@ -0,0 +1,10 @@
import bench
def test(num):
for i in iter(range(num // 10000)):
l = [0] * 1000
l2 = tuple(map(lambda x: x, l))
bench.run(test)

View File

@@ -0,0 +1,10 @@
import bench
def test(num):
for i in iter(range(num // 10000)):
l = [0] * 1000
l2 = bytes(l)
bench.run(test)

View File

@@ -0,0 +1,10 @@
import bench
def test(num):
for i in iter(range(num // 10000)):
l = [0] * 1000
l2 = bytes(map(lambda x: x, l))
bench.run(test)

View File

@@ -0,0 +1,10 @@
import bench
def test(num):
for i in iter(range(num // 10000)):
l = [0] * 1000
l2 = bytearray(l)
bench.run(test)

View File

@@ -0,0 +1,10 @@
import bench
def test(num):
for i in iter(range(num // 10000)):
l = [0] * 1000
l2 = bytearray(map(lambda x: x, l))
bench.run(test)

View File

@@ -0,0 +1,13 @@
import bench
def func(a):
pass
def test(num):
for i in iter(range(num)):
func(i)
bench.run(test)

View File

@@ -0,0 +1,13 @@
import bench
def func(a, b, c):
pass
def test(num):
for i in iter(range(num)):
func(i, i, i)
bench.run(test)

View File

@@ -0,0 +1,13 @@
import bench
def func(a, b=1, c=2):
pass
def test(num):
for i in iter(range(num)):
func(i)
bench.run(test)

View File

@@ -0,0 +1,13 @@
import bench
def func(a):
pass
def test(num):
for i in iter(range(num)):
func(a=i)
bench.run(test)

View File

@@ -0,0 +1,13 @@
import bench
def func(a, b, c):
pass
def test(num):
for i in iter(range(num)):
func(c=i, b=i, a=i)
bench.run(test)

View File

@@ -0,0 +1,9 @@
import bench
def test(num):
for i in iter(range(num // 20)):
enumerate([1, 2], 1)
bench.run(test)

View File

@@ -0,0 +1,9 @@
import bench
def test(num):
for i in iter(range(num // 20)):
enumerate(iterable=[1, 2], start=1)
bench.run(test)

View File

@@ -0,0 +1,11 @@
# Function call overhead test
# Establish a baseline for performing a trivial operation inline
import bench
def test(num):
for i in iter(range(num)):
a = i + 1
bench.run(test)

View File

@@ -0,0 +1,15 @@
# Function call overhead test
# Perform the same trivial operation as global function call
import bench
def f(x):
return x + 1
def test(num):
for i in iter(range(num)):
a = f(i)
bench.run(test)

View File

@@ -0,0 +1,19 @@
# Function call overhead test
# Perform the same trivial operation as calling function, cached in a
# local variable. This is commonly known optimization for overly dynamic
# languages (the idea is to cut on symbolic look up overhead, as local
# variables are accessed by offset, not by name)
import bench
def f(x):
return x + 1
def test(num):
f_ = f
for i in iter(range(num)):
a = f_(i)
bench.run(test)

View File

@@ -0,0 +1,9 @@
import bench
def test(num):
for i in range(num):
pass
bench.run(test)

View File

@@ -0,0 +1,9 @@
import bench
def test(num):
for i in iter(range(num)):
pass
bench.run(test)

View File

@@ -0,0 +1,10 @@
import bench
def test(num):
i = 0
while i < num:
i += 1
bench.run(test)

View File

@@ -0,0 +1,9 @@
import bench
def test(num):
while num > 0:
num -= 1
bench.run(test)

View File

@@ -0,0 +1,9 @@
import bench
def test(num):
while num != 0:
num -= 1
bench.run(test)

View File

@@ -0,0 +1,10 @@
import bench
def test(num):
zero = 0
while num != zero:
num -= 1
bench.run(test)

View File

@@ -0,0 +1,10 @@
import bench
def test(num):
i = 0
while i < 20000000:
i += 1
bench.run(test)

View File

@@ -0,0 +1,12 @@
import bench
ITERS = 20000000
def test(num):
i = 0
while i < ITERS:
i += 1
bench.run(test)

View File

@@ -0,0 +1,11 @@
import bench
def test(num):
ITERS = 20000000
i = 0
while i < ITERS:
i += 1
bench.run(test)

View File

@@ -0,0 +1,10 @@
import bench
def test(num):
i = 0
while i < num:
i += 1
bench.run(lambda n: test(20000000))

View File

@@ -0,0 +1,14 @@
import bench
class Foo:
num = 20000000
def test(num):
i = 0
while i < Foo.num:
i += 1
bench.run(test)

View File

@@ -0,0 +1,16 @@
import bench
class Foo:
def __init__(self):
self.num = 20000000
def test(num):
o = Foo()
i = 0
while i < o.num:
i += 1
bench.run(test)

View File

@@ -0,0 +1,20 @@
import bench
class Foo:
def __init__(self):
self.num1 = 0
self.num2 = 0
self.num3 = 0
self.num4 = 0
self.num = 20000000
def test(num):
o = Foo()
i = 0
while i < o.num:
i += 1
bench.run(test)

View File

@@ -0,0 +1,19 @@
import bench
class Foo:
def __init__(self):
self._num = 20000000
def num(self):
return self._num
def test(num):
o = Foo()
i = 0
while i < o.num():
i += 1
bench.run(test)

View File

@@ -0,0 +1,14 @@
import bench
from ucollections import namedtuple
T = namedtuple("Tup", ["num", "bar"])
def test(num):
t = T(20000000, 0)
i = 0
while i < t.num:
i += 1
bench.run(test)

View File

@@ -0,0 +1,14 @@
import bench
from ucollections import namedtuple
T = namedtuple("Tup", ["foo1", "foo2", "foo3", "foo4", "num"])
def test(num):
t = T(0, 0, 0, 0, 20000000)
i = 0
while i < t.num:
i += 1
bench.run(test)