MicroPython 在不同的硬件上性能测试数据,为选择性能合适的硬件提供参考。
测试的开发板:
micro:bit
PYB Nano
PYBV10
ST Nucleo_F091RC
ST Nucleo_F411RE
STM32L476DISC
STM32F7DISC
ST Nucleo_H743ZI
ESP8266
ESP32 WROVER
MicroPython 版本
microbit
1.9.2
ESP32 Lobo
3.20.20
All other device
1.9.4-479
测试项目
Integer addition 1000,000 times
Float addition 1000,000 times
Integer multiplication 1000,000 times
Float multiplication 1000,000 times
Integer division 1000,000 times
Float division 1000,000 times
1000 digit Pi calculation
5000 digit Pi calculation
100,000 digit Pi calculation
结果
MCU
Freq
Int Add
Float Add
Int Mul
Float Mul
Int div
Float Div
Pi:1000
Pi:5000
Pi:100000
microbit
nRF51822
16M
61.89
78.03
71.59
81.60
67.95
106.87
10.98
–
–
Nucleo_F411
STM32F411
96M
5.86
13.96
6.07
14.02
6.07
14.07
1.25
19.03
–
PYBV10
STM32F405
168M
3.44
7.93
3.56
7.97
3.56
8.13
0.67
10.8
–
Nucleo_L432KC
STM32LM32
32M
20.86
46.35
21.49
46.55
21.95
46.71
2.60
49.44
–
STM32L476DISC
STM32L476
80M
8.59
18.34
8.99
18.42
8.93
18.49
1.37
21.45
–
STM32F7DISC
STM32F746
192M
1.93
5.16
2.45
5.08
2.12
5.39
0.21
5.42
4276.47
Nucleo_H743
STM32H743
400M
0.86
1.96
0.94
1.98
0.91
2.07
0.11
4.66
1004.32
ESP8266
ESP8266
80M
15.55
18.34
17.96
18.92
16.96
21.46
2.09
40.22
–
ESP32
ESP32
240M
2.61
4.42
2.79
4.42
2.72
4.66
0.57
8.41
–
ESP32 psRAM
ESP32
240M
3.37
7.96
3.55
17.88
15.25
8.32
0.67
18.01
12394.50
K210
K210C
8.19
8.76
8.23
8.74
7.75
8.76
0.12
2.82
1480.96
'''
File: benchmarks.py
Descript: benchmark test for different microcontroller
Author: Shao ziyang
Data: 2018-Octo-25
Version: 1.0
'''
import time
import machine
import gc
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_test(n = 5000):
t1 = time.ticks_ms()
t = pi(n)
t2 = time.ticks_ms()
r = time.ticks_diff(t2, t1)/1000
print(' Pi', n, 'digit calculation: ', r, 's')
return '%.2f'%r
def int_add_test(n = 1000000, a = 12345, b = 56789):
t1 = time.ticks_ms()
sum = 0
for i in range(n):
sum = a + b
t2 = time.ticks_ms()
r = time.ticks_diff(t2, t1)/1000
print(' Integer Add test', n, 'times: ', r, 's')
return '%.2f'%r
def float_add_test(n=1000000, a = 1234.5678, b = 5678.1234):
t1 = time.ticks_ms()
sum = 0
for i in range(n):
sum = a + b
t2 = time.ticks_ms()
r = time.ticks_diff(t2, t1)/1000
print(' Float Add test', n, 'times:', r, 's')
return '%.2f'%r
def int_mul_test(n=1000000, a = 12345, b = 56789):
t1 = time.ticks_ms()
sum = 0
for i in range(n):
sum = a * b
t2 = time.ticks_ms()
r = time.ticks_diff(t2, t1)/1000
print(' Integer Mul test', n, 'times: ', r, 's')
return '%.2f'%r
def float_mul_test(n=1000000, a = 1234.5678, b = 5678.1234):
t1 = time.ticks_ms()
sum = 0
for i in range(n):
sum = a * b
t2 = time.ticks_ms()
r = time.ticks_diff(t2, t1)/1000
print(' Float Mul test', n, 'times: ', r, 's')
return '%.2f'%r
def int_div_test(n=1000000, a = 123456, b = 567):
t1 = time.ticks_ms()
sum = 0
for i in range(n):
sum = a // b
t2 = time.ticks_ms()
r = time.ticks_diff(t2, t1)/1000
print(' Integer Div test', n, 'times: ', r, 's')
return '%.2f'%r
def float_div_test(n=1000000, a = 12345.678, b = 56.789):
t1 = time.ticks_ms()
sum = 0
for i in range(n):
sum = a / b
t2 = time.ticks_ms()
r = time.ticks_diff(t2, t1)/1000
print(' Float Div test', n, 'times: ', r, 's')
return '%.2f'%r
def mem():
r = gc.mem_free()
print('free memory:', r)
print('Speed test')
try:
print('System freq: {:.1f} MHz'.format(machine.freq()[0]/1000000))
except:
print('System freq: {:.1f} MHz'.format(machine.freq()/1000000))
print('\nCalcaulate integer addition')
gc.collect()
mem()
d1 = int_add_test()
d2 = int_add_test()
d3 = int_add_test()
r_int_add = min(d1, d2, d3)
print('Integer addition test result: ', r_int_add, 's')
mem()
print('\nCalcaulate float addition')
gc.collect()
mem()
d1 = float_add_test()
d2 = float_add_test()
d3 = float_add_test()
r_float_add = min(d1, d2, d3)
print('Float addition test result: ', r_float_add, 's')
mem()
print('\nCalcaulate integer multiplication')
gc.collect()
mem()
d1 = int_mul_test()
d2 = int_mul_test()
d3 = int_mul_test()
r_int_mul = min(d1, d2, d3)
print('Integer multiplication test result: ', r_int_mul, 's')
mem()
print('\nCalcaulate float multiplication')
gc.collect()
mem()
d1 = float_mul_test()
d2 = float_mul_test()
d3 = float_mul_test()
r_float_mul = min(d1, d2, d3)
print('Float multiplication test result: ', r_float_mul, 's')
mem()
print('\nCalcaulate integer division')
gc.collect()
mem()
d1 = int_div_test()
d2 = int_div_test()
d3 = int_div_test()
r_int_div = min(d1, d2, d3)
print('Integer division test result: ', r_int_div, 's')
mem()
print('\nCalcaulate float division')
gc.collect()
mem()
d1 = float_div_test()
d2 = float_div_test()
d3 = float_div_test()
r_float_div = min(d1, d2, d3)
print('Float division test result: ', r_float_div, 's')
mem()
print('\nCalcaulate Pi 1000 digit')
gc.collect()
mem()
try:
d1 = pi_test(1000)
d2 = pi_test(1000)
d3 = pi_test(1000)
r_pi_1000 = min(d1, d2, d3)
print('1000 digit Pi calculation result: ', r_pi_1000, 's')
mem()
except:
r_pi_1000 = None
print(' calculation error')
print('\nCalcaulate Pi 5000 digit')
gc.collect()
mem()
try:
d1 = pi_test(5000)
d2 = pi_test(5000)
d3 = pi_test(5000)
r_pi_5000 = min(d1, d2, d3)
print('5000 digit Pi calculation result: ', r_pi_5000, 's')
mem()
except:
r_pi_5000 = None
print(' calculation error')
print('\nCalcaulate Pi 100,000 digit')
gc.collect()
mem()
try:
d1 = pi_test(100000)
d2 = pi_test(100000)
d3 = pi_test(100000)
r_pi_100000 = min(d1, d2, d3)
print('100000 digit Pi calculation result: ', r_pi_100000, 's')
mem()
except:
r_pi_100000 = None
print(' calculation error')
print('Test result:')
print(' Integer addition test result: ', r_int_add, 's')
print(' Float addition test result: ', r_float_add, 's')
print(' Integer multiplication test result: ', r_int_mul, 's')
print(' Float multiplication test result: ', r_float_mul, 's')
print(' Integer division test result: ', r_int_div, 's')
print(' Float division test result: ', r_float_div, 's')
if r_pi_1000:
print(' 1000 digit Pi calculation result: ', r_pi_1000, 's')
if r_pi_5000:
print(' 5000 digit Pi calculation result: ', r_pi_5000, 's')
if r_pi_100000:
print(' 100000 digit Pi calculation result: ', r_pi_100000, 's')