Scroungre wrote: ↑
Thu Sep 28, 2023 6:38 am
Incidentally, @micropython.native does speed up the code and doesn't throw errors - but it's still not fast enough. And we're talking by rather a lot - shaving a few percent here and there isn't going to help. I'm moving to 'C' in hopes of at least 10x faster. Even that'll be marginal.
MicroPython is by default compiled to bytecode which is then interpreted. The generally accepted rule of thumb is it will be about 100 times slower than the equivalent code written in C. Using 'native' and 'viper' code generation will move that towards being about twice as fast, perhaps more if the code suits that.
Much will depend on exactly what one is doing and there can still be interpreter overhead in calling the 'native' and 'viper' code and those calling interpreted functions. The more one can put in a 'native' or 'viper' routine, the longer one can stay within those, the greater the gains will usually be.
I did some benchmarking here - viewtopic.php?t=303458
- That was a while ago and both MicroPython and desktop Python have improved over time, but I would expect it's still in the right ballpark. It's an artificial benchmark and most real world code probably won't see such great 'native' and 'viper' benefits.
Python and MicroPython's virtues are in making coding easier, and that come at the cost of being interpreted and being slower to execute. There are Python compilers but they are usually only available for 'desktop platforms'.
If one does need fast execution, Python and MicroPython may not be the solutions and one may need to use C, and may need to optimise that, overclock the RP2040. If the RP2040 isn't fast enough one may have to consider a faster microcontroller or an SBC SoC, using a Pi Zero or a faster Pi variant.
Scroungre wrote: ↑
Thu Sep 28, 2023 10:36 am
What it is supposed to do is real-time system control, and the update rate should be about 100kHz.
The same algorithm implemented in basic Python, and on the Pico (default clock, 125MHz), ran at about 1 kHz. With .native, and other optimizations, I got it to about 1.8kHz.
100kHz means getting everything done in 10us; that's about 1,250 instructions on an RP2040 at default clock speed.
If you can describe the functionality needed then people here would be better placed to offer advice, determine the feasibility of using an RP2040 or what you would need, whether things like PIO and the interpolator may be helpful in what you are doing.