/edit
WARNING:
Writting floats doesn't write the values correctly. See posts below.
Ok, I ran some tests:
Code: Select all
file type, storage type, data type
binary, flash, int: 1000 lines 1.88 s 529 Hz
binary, flash, float: 1000 lines 1.89 s 528 Hz
string, flash, int: 1000 lines 4.72 s 211 Hz
string, flash, float: 1000 lines 5.79 s 172 Hz
binary, sd, int: 1000 lines 0.05 s 21256 Hz
binary, sd, float: 1000 lines 0.05 s 20810 Hz
string, sd, int: 1000 lines 0.48 s 2055 Hz
string, sd, float: 1000 lines 0.37 s 2700 Hz
Ran them a couple of times, the results didn't vary much.
- int is little faster than float, but not much
- binary is way faster than text, 2x faster on flash, 10x faster on sd
- sd is way faster than flash, 20x faster in binary, 10x faster in string
So, if you need to store fast:
- store to sd
- store in binary format
- store int or float, ints are not faster than floats, so converting is a waste of cycles
I also ran the fastest option (binary sd int) with the native emitter:
Code: Select all
binary sd int native: 1000 lines 0.03796387 s 26314.49 Hz
A nice 20% speed bump for free.
Viper however didn't run, unless I removed the file operations, so it seems like viper can't write to files. Also for viper you typically have to make some changes. 1+1.0 doesn't work, only 1+1 or 1.0+1.0. When the function without file writing in native and viper, viper gave a speed pump of about 7%.
test code:
Code: Select all
# main.py -- put your code here!
import pyb
import gc
def write_binary_flash(inp, no):
t_start = pyb.millis()/1000
log = open('/flash/testlog.bin', 'wb')
for i in range(no):
b = bytes([i, inp, '\n'])
log.write(b)
log.close()
dt = pyb.millis()/1000 - t_start
print(i+1, 'lines', dt, 's', i/dt, 'Hz')
def write_str_flash(inp, no):
t_start = pyb.millis()/1000
log = open('/flash/testlog.txt', 'w')
for i in range(no):
string = '{}{}\n'.format(i, inp)
log.write(string)
log.close()
dt = pyb.millis()/1000 - t_start
print(i+1, 'lines', dt, 's', i/dt, 'Hz')
def write_binary_sd(inp, no):
t_start = pyb.millis()/1000
log = open('/sd/testlog.bin', 'wb')
for i in range(no):
b = bytes([i, inp, '\n'])
log.write(b)
log.close()
dt = pyb.millis()/1000 - t_start
print(i+1, 'lines', dt, 's', i/dt, 'Hz')
def write_str_sd(inp, no):
t_start = pyb.millis()/1000
log = open('/sd/testlog.txt', 'w')
for i in range(no):
string = '{}{}\n'.format(i, inp)
log.write(string)
log.close()
dt = pyb.millis()/1000 - t_start
print(i+1, 'lines', dt, 's', i/dt, 'Hz')
@micropython.native
def write_binary_sd_native(inp, no):
t_start = pyb.millis()/1000
log = open('/sd/testlog.bin', 'wb')
for i in range(no):
b = bytes([i, inp, '\n'])
log.write(b)
log.close()
dt = pyb.millis()/1000 - t_start
print(i+1, 'lines', dt, 's', i/dt, 'Hz')
@micropython.viper
def write_binary_sd_viper(inp:int, no:int):
t_start = pyb.millis()/1000.0
# log = open('/sd/testlog.bin', 'wb')
for i in range(no):
b = bytes([i, inp, '\n'])
# log.write(b)
# log.close()
dt = pyb.millis()/1000.0 - t_start
ifl = float(i)
print(i+1, 'lines', dt, 's', ifl/dt, 'Hz')
print('binary flash:', end='\t\t')
gc.collect()
write_binary_flash(973, 1000)
print('string flash:', end='\t\t')
gc.collect()
write_str_flash(973, 1000)
print('binary flash float:', end='\t')
gc.collect()
write_binary_flash(973.1, 1000)
print('string flash float:', end='\t')
gc.collect()
write_str_flash(973.1, 1000)
print('binary sd:', end='\t\t')
gc.collect()
write_binary_sd(973, 1000)
print('string sd:', end='\t\t')
gc.collect()
write_str_sd(973, 1000)
print('binary sd float:', end='\t')
gc.collect()
write_binary_sd(973.1, 1000)
print('string sd float:', end='\t')
gc.collect()
write_str_sd(973.1, 1000)
print('binary sd int native:', end='\t')
gc.collect()
write_binary_sd_native(973, 1000)
print('binary sd int viper:', end='\t')
gc.collect()
write_binary_sd_viper(973, 1000)
print('but viper can\'t write files')