Created
January 24, 2022 02:11
-
-
Save scoates/41bd9af6f611dc42292961acfbb136fa to your computer and use it in GitHub Desktop.
Userspace is slow? Especially on microcontrollers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import os | |
try: | |
sysname = os.uname().sysname | |
except AttributeError: | |
sysname = "micropython-darwin" | |
if sysname in ["esp32", "micropython-darwin"]: | |
# micropython | |
print(f"micropython {sysname}") | |
from random import getrandbits | |
from utime import ticks_us, ticks_diff | |
from time import sleep | |
def rand8(): | |
getrandbits(8) | |
else: | |
# cpython | |
print(f"cpython: {sysname}") | |
from random import random | |
from math import floor | |
from time import time, sleep | |
def rand8(): | |
return floor(random() * 255) | |
def ticks_us(): | |
return round(time() * 1000 * 1000) | |
def ticks_diff(t2, t1): | |
return t2 - t1 | |
pixels = [] | |
for p in range(255): | |
pix = [] | |
for i in range(3): | |
pix.append(rand8()) | |
pixels.append(pix) | |
# before = ticks_us() | |
# sleep(1) | |
# print(f"{ticks_diff(ticks_us(), before)} usec") | |
def handroll(pixels): | |
ret = "pixels = [" | |
for p in pixels: | |
ret += f"[{p[0]}, {p[1]}, {p[2]}]," | |
def jsonroll(pixels): | |
ret = "pixels = [" | |
ret += json.dumps(pixels) | |
before = ticks_us() | |
handroll(pixels) | |
print(f"handroll time: {ticks_diff(ticks_us(), before)} usec") | |
before = ticks_us() | |
jsonroll(pixels) | |
print(f"jsonroll time: {ticks_diff(ticks_us(), before)} usec") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cpython: Darwin | |
handroll time: 149 usec | |
jsonroll time: 151 usec | |
---- | |
micropython micropython-darwin | |
handroll time: 2320 usec | |
jsonroll time: 1257 usec | |
---- | |
micropython esp32 | |
handroll time: 2183061 usec | |
jsonroll time: 22771 usec |
FWIW:
micropython esp32 with @micropython.native
decorator on handroll
handroll time: 2155378 usec
jsonroll time: 17526 usec
micropython esp32 with @micropython.viper
decorator on handroll
handroll time: 2170409 usec
jsonroll time: 16919 usec
I bet this is actually a memory allocation problem, based on this maximizing speed document.
There's probably some hinting we can do with e.g. bytearray
and memoryview
instead of string reallocation, but I think I'm going to push the heavy lifting out to another system (the visiting browser user-agent in this case).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Almost 100x faster for 256 elements on micropython esp32!
(cpython is just about the same speed, and micropython-darwin is ~2x)