Eudimorphodon
Veteran Member
Thanks, I forgot about that. I was aware that they're used as a cache for imported modules, but this stackoverflow question explains how you can compile the main executable scripts too, and then run them via the interpreter. Unfortunately that has the small drawback that you now have to runpython3 foo.pyc
instead of (if your file had a#!/usr/bin/env python3
or similar line) just./foo.py
.
.pyc files aren’t always smaller than the original source; it’s not usual at all for them to actually be larger. The point of them isn’t to ‘shrink’ the code, it’s to save the effort of repeatedly converting that nasty human readable junk into byte code that can be directly executed by the virtual machine. Full-fat Python simply is not designed to care much about optimizing for memory savings over performance.
MicroPython/Circuitpython *do* have a size-optimized compiled file format; code processed this way has the .mpy extension:
MicroPython .mpy files — MicroPython latest documentation
docs.micropython.org
This format offers a few interesting options, including in some cases the ability to directly embed native machine code modules…
But per my original point, if you’re *compiling* things for your microcontroller, well… MicroPython needs a *minimum* of 256k of flash before you even get to your user code. That’s 256k of flash for “hello world” no matter what you do. With C you can get that done in little more than assembly would require.
(As for performance, well… I’ve done some bit-bang-y work with microcontrollers where I resorted to embedded assembly because a loop in C required just one or two more processor clock ticks than the hand-tuned ASM version… we’re literally talking about the difference between 7 and 9 ticks per iteration. Something tells me it’s not going to be that close with MicroPython.)