SDCARD performance

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
thezim
 
Posts: 7
Joined: Fri Jun 16, 2017 3:37 pm

SDCARD performance

Post by thezim »

It there anyway to squeeze more performance out of sdcardio under CircuitPython?

The setup:

Sparkfun Thing+ RP2040
PNY 16GB Elite Class 10 MicroSDHC

Code:

Code: Select all

import os
import board
import sdcardio
import storage
import busio
import random
import io
import time

ITERATIONS = 100
BUFFER_SIZE = 512

times = [0] * ITERATIONS
sum = 0

spi = busio.SPI(board.SD_SCK, MOSI=board.SD_MOSI, MISO=board.SD_MISO)
sdcard = sdcardio.SDCard(spi, board.SD_CS, baudrate=50_000_000)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, '/sd')
logfile = open("/sd/test.bin", "wb")

for i in range(0, ITERATIONS):
    data = bytes(os.urandom(BUFFER_SIZE))
    start = time.monotonic_ns()
    logfile.write(data)
    stop = time.monotonic_ns()
    elapsed_ms = (stop - start) / 1_000_000
    times[i] = elapsed_ms
    sum = sum + elapsed_ms
    print(f"Iteration: {i + 1} Length: {len(data)} elapsed: {elapsed_ms}ms")

logfile.close()
storage.umount(vfs)
vfs.umount()

times.sort()
med = times[ITERATIONS // 2]
avg = sum / ITERATIONS
max = times[ITERATIONS - 1]
min = times[0]

print(f"\nMedian: {med} Average: {avg} Minimum: {min} Maximum: {max}")
Output:

Code: Select all

Iteration: 1 Length: 512 elapsed: 2.92969ms
Iteration: 2 Length: 512 elapsed: 1.95313ms
Iteration: 3 Length: 512 elapsed: 1.95313ms
Iteration: 4 Length: 512 elapsed: 1.95313ms
Iteration: 5 Length: 512 elapsed: 1.95313ms
Iteration: 6 Length: 512 elapsed: 1.95313ms
Iteration: 7 Length: 512 elapsed: 2.92969ms
Iteration: 8 Length: 512 elapsed: 1.95313ms
Iteration: 9 Length: 512 elapsed: 1.95313ms
Iteration: 10 Length: 512 elapsed: 1.95313ms
Iteration: 11 Length: 512 elapsed: 1.95313ms
Iteration: 12 Length: 512 elapsed: 2.92969ms
Iteration: 13 Length: 512 elapsed: 2.92969ms
Iteration: 14 Length: 512 elapsed: 1.95313ms
Iteration: 15 Length: 512 elapsed: 1.95313ms
Iteration: 16 Length: 512 elapsed: 2.92969ms
Iteration: 17 Length: 512 elapsed: 1.95313ms
Iteration: 18 Length: 512 elapsed: 1.95313ms
Iteration: 19 Length: 512 elapsed: 1.95313ms
Iteration: 20 Length: 512 elapsed: 1.95313ms
Iteration: 21 Length: 512 elapsed: 1.95313ms
Iteration: 22 Length: 512 elapsed: 1.95313ms
Iteration: 23 Length: 512 elapsed: 2.92969ms
Iteration: 24 Length: 512 elapsed: 2.92969ms
Iteration: 25 Length: 512 elapsed: 1.95313ms
Iteration: 26 Length: 512 elapsed: 1.95313ms
Iteration: 27 Length: 512 elapsed: 1.95313ms
Iteration: 28 Length: 512 elapsed: 1.95313ms
Iteration: 29 Length: 512 elapsed: 1.95313ms
Iteration: 30 Length: 512 elapsed: 1.95313ms
Iteration: 31 Length: 512 elapsed: 1.95313ms
Iteration: 32 Length: 512 elapsed: 1.95313ms
Iteration: 33 Length: 512 elapsed: 1.95313ms
Iteration: 34 Length: 512 elapsed: 1.95313ms
Iteration: 35 Length: 512 elapsed: 1.95313ms
Iteration: 36 Length: 512 elapsed: 1.95313ms
Iteration: 37 Length: 512 elapsed: 1.95313ms
Iteration: 38 Length: 512 elapsed: 1.95313ms
Iteration: 39 Length: 512 elapsed: 1.95313ms
Iteration: 40 Length: 512 elapsed: 1.95313ms
Iteration: 41 Length: 512 elapsed: 1.95313ms
Iteration: 42 Length: 512 elapsed: 1.95313ms
Iteration: 43 Length: 512 elapsed: 1.95313ms
Iteration: 44 Length: 512 elapsed: 1.95313ms
Iteration: 45 Length: 512 elapsed: 1.95313ms
Iteration: 46 Length: 512 elapsed: 2.92969ms
Iteration: 47 Length: 512 elapsed: 2.92969ms
Iteration: 48 Length: 512 elapsed: 1.95313ms
Iteration: 49 Length: 512 elapsed: 2.92969ms
Iteration: 50 Length: 512 elapsed: 2.92969ms
Iteration: 51 Length: 512 elapsed: 1.95313ms
Iteration: 52 Length: 512 elapsed: 1.95313ms
Iteration: 53 Length: 512 elapsed: 1.95313ms
Iteration: 54 Length: 512 elapsed: 1.95313ms
Iteration: 55 Length: 512 elapsed: 1.95313ms
Iteration: 56 Length: 512 elapsed: 1.95313ms
Iteration: 57 Length: 512 elapsed: 1.95313ms
Iteration: 58 Length: 512 elapsed: 7.8125ms
Iteration: 59 Length: 512 elapsed: 1.95313ms
Iteration: 60 Length: 512 elapsed: 1.95313ms
Iteration: 61 Length: 512 elapsed: 1.95313ms
Iteration: 62 Length: 512 elapsed: 2.92969ms
Iteration: 63 Length: 512 elapsed: 2.92969ms
Iteration: 64 Length: 512 elapsed: 2.92969ms
Iteration: 65 Length: 512 elapsed: 1.95313ms
Iteration: 66 Length: 512 elapsed: 1.95313ms
Iteration: 67 Length: 512 elapsed: 1.95313ms
Iteration: 68 Length: 512 elapsed: 1.95313ms
Iteration: 69 Length: 512 elapsed: 2.92969ms
Iteration: 70 Length: 512 elapsed: 1.95313ms
Iteration: 71 Length: 512 elapsed: 1.95313ms
Iteration: 72 Length: 512 elapsed: 1.95313ms
Iteration: 73 Length: 512 elapsed: 1.95313ms
Iteration: 74 Length: 512 elapsed: 2.92969ms
Iteration: 75 Length: 512 elapsed: 1.95313ms
Iteration: 76 Length: 512 elapsed: 1.95313ms
Iteration: 77 Length: 512 elapsed: 1.95313ms
Iteration: 78 Length: 512 elapsed: 1.95313ms
Iteration: 79 Length: 512 elapsed: 1.95313ms
Iteration: 80 Length: 512 elapsed: 1.95313ms
Iteration: 81 Length: 512 elapsed: 1.95313ms
Iteration: 82 Length: 512 elapsed: 1.95313ms
Iteration: 83 Length: 512 elapsed: 1.95313ms
Iteration: 84 Length: 512 elapsed: 1.95313ms
Iteration: 85 Length: 512 elapsed: 2.92969ms
Iteration: 86 Length: 512 elapsed: 2.92969ms
Iteration: 87 Length: 512 elapsed: 2.92969ms
Iteration: 88 Length: 512 elapsed: 1.95313ms
Iteration: 89 Length: 512 elapsed: 1.95313ms
Iteration: 90 Length: 512 elapsed: 1.95313ms
Iteration: 91 Length: 512 elapsed: 1.95313ms
Iteration: 92 Length: 512 elapsed: 1.95313ms
Iteration: 93 Length: 512 elapsed: 1.95313ms
Iteration: 94 Length: 512 elapsed: 1.95313ms
Iteration: 95 Length: 512 elapsed: 1.95313ms
Iteration: 96 Length: 512 elapsed: 1.95313ms
Iteration: 97 Length: 512 elapsed: 1.95313ms
Iteration: 98 Length: 512 elapsed: 2.92969ms
Iteration: 99 Length: 512 elapsed: 1.95313ms
Iteration: 100 Length: 512 elapsed: 1.95313ms

Median: 1.95313 Average: 2.20703 Minimum: 1.95313 Maximum: 7.8125

User avatar
thezim
 
Posts: 7
Joined: Fri Jun 16, 2017 3:37 pm

Re: SDCARD performance

Post by thezim »

I ask because I have busy can bus (Tesla Model 3) with a median period of 351 microseconds between can frames. 1.9ms is way too long to without losing frames. Even if buffered to 512 byes I'm not even sure under CircuitPython or MicroPython that write call here is a blocking, preventing the ability to even buffer.

User avatar
blakebr
 
Posts: 957
Joined: Tue Apr 17, 2012 6:23 pm

Re: SDCARD performance

Post by blakebr »

Zim,

Take a look at the Raspberry Pi Zero 2 W. It is much faster than the RP2040
There is a version of Circuit Python that will run on it.
Python 3 comes with the OS that is native to the Raspberry Pi Series.
Good Luck with your project.

Bruce

adafruit-circuitpython-raspberrypi_zero2w-en_US-20220113-936c9b2.disk.img.zip

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”