Think MMBasic is slow? Think again. CSub to the rescue

I’ve always loved the Mandelbrot set and the stock picocalc comes with a Mandelbrot set generator. But it’s slow. Very slow.

Interpreted languages just aren’t good at math intensive code in a tight loop. So I played around trying to optimize the basic but could only get so far.

Then I discovered that you can write code in C and call it from MMbasic. I tried it out and found about a 7-10x speed up.

Making a CSub is a non trivial poorly documented process. So I bundled up what I learned on GitHub along with some versions of a much better Mandelbrot demo.

The code is available here. GitHub - jvanderberg/picocalc_csub_helpers: PicoMite CSub helper scripts

Note that the demo programs embed processor dependent machine code into the CSub definition, so you need to build for the correct mcu, rp2040 or rp2350. There’s premade versions in the ‘builds’ directory.

17 Likes

Thanks so much for pointing that out — it really sounds fascinating!

Pretty much all the old BASICs allowed you to create and call an assembly language program in memory as a subprogram. The mechanism differed by BASIC variant and changed over time, but it was a common way to make a subroutine that allowed you to do something very fast.

2 Likes