Skip to content

Instantly share code, notes, and snippets.

@scoates
Created January 24, 2022 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scoates/41bd9af6f611dc42292961acfbb136fa to your computer and use it in GitHub Desktop.
Save scoates/41bd9af6f611dc42292961acfbb136fa to your computer and use it in GitHub Desktop.
Userspace is slow? Especially on microcontrollers
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")
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
@scoates
Copy link
Author

scoates commented Jan 24, 2022

Almost 100x faster for 256 elements on micropython esp32!
(cpython is just about the same speed, and micropython-darwin is ~2x)

@scoates
Copy link
Author

scoates commented Jan 24, 2022

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