和其它MCU一样,我们可以用计算圆周率测试RP2040性能。
import gc
from utime import ticks_ms, ticks_diff
def pi(places=100):
# 3 + 3*(1/24) + 3*(1/24)*(9/80) + 3*(1/24)*(9/80)*(25/168)
# The numerators 1, 9, 25, ... are given by (2x + 1) ^ 2
# The denominators 24, 80, 168 are given by (16x^2 -24x + 8)
extra = 8
one = 10 ** (places+extra)
t, c, n, na, d, da = 3*one, 3*one, 1, 0, 0, 24
while t > 1:
n, na, d, da = n+na, na+8, d+da, da+32
t = t * n // d
c += t
return c // (10 ** extra)
def pi_t(n=1000):
gc.collect()
t1 = ticks_ms()
pi(n)
t2 = ticks_ms()
print(' ', ticks_diff(t2, t1), 'ms')
for i in (100, 500, 1000, 5000, 10000):
try:
print('\nCalc {} bits pi'.format(i))
pi_t(i)
except:
print('Calc error!')
运行结果
Calc 100 bits pi
17 ms
Calc 500 bits pi
190 ms
Calc 1000 bits pi
665 ms
Calc 5000 bits pi
19950 ms
Calc 10000 bits pi
74292 ms
可以看到M0+内核的RP2040,计算性能和STM32F411接近,比STM32F405低,但比同是M0+内核的nRF51822强很多。