Skip to content

Instantly share code, notes, and snippets.

@ksasao
Last active March 24, 2022 04:54
Show Gist options
  • Save ksasao/3322e2c1508dbbdceb955ef753b69f3a to your computer and use it in GitHub Desktop.
Save ksasao/3322e2c1508dbbdceb955ef753b69f3a to your computer and use it in GitHub Desktop.
MaixPy 0.5.0_9 で利用できるようになったNumPy相当の機能のテスト。2つの768次元ベクトルの距離の二乗を計算。ファームウェア http://dl.sipeed.com/MAIX/MaixPy/release/master/maixpy_v0.5.0_9_g8eba07d マニュアル https://micropython-ulab.readthedocs.io/en/latest/ulab.html
import ulab as np
import random
import time
# 初期化
clock = time.clock()
a = []
b = []
for i in range(768):
a.append(random.random())
b.append(random.random())
print(a)
print(b)
# 普通に距離の二乗を計算
def distance(a,b):
l = len(a)
s = 0
for i in range(l):
s = s + (a[i]-b[i])**2
return s
# NumPy利用 (np.linalg.normは無いっぽい?)
def npdistance(a,b):
return np.sum((a-b)*(a-b))
start = time.ticks_us()
for i in range(100):
distance(a,b)
print("Python: " + str(time.ticks_diff(time.ticks_us(), start)) + " us")
na = np.array(a)
nb = np.array(b)
start = time.ticks_us()
for i in range(100):
npdistance(na,nb)
print("NumPy: " + str(time.ticks_diff(time.ticks_us(), start)) + " us")
Python: 584740 us
NumPy: 63624 us
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment