C12 · Lesson 49 · Comparing MicroPython and C
Speed, size, timing, and the time it takes to write
This course has written every kind of program twice. The differences are now concrete: C toggles a pin in under a microsecond, MicroPython in 40; a C program for a small job is a hundred bytes, while MicroPython needs its whole interpreter on the chip before the first line runs; C's interrupt handlers respond in half a microsecond, MicroPython's soft ones whenever the interpreter next looks. Against that, MicroPython programs are shorter, easier to change while the board runs, and much more forgiving to write. Choosing between them, or using both, is an engineering decision with numbers behind it.
- The same job in both languages: toggle a pin 100 times and print how long it took, measured by the board's own clock. The C version prints 705 cycles, 70 µs.
- The MicroPython version, below, prints 4000 µs: 57 times longer, nearly all of it the 40 µs each call into the hardware costs.
- Open the C version's compiled program: the whole thing is 27 instructions, 108 bytes.
Predict
A job makes 2000 hardware calls. Roughly how long does it take in MicroPython on this board, and in C at about 7 cycles per call?
80 ms in MicroPython ( µs) and 1.4 ms in C ( µs). The hardware is the same; the difference is the work around each access: MicroPython looks up the method, checks the arguments' types and converts the values on every call, while C's compiled code is just a store.
Name it
Speed
Each MicroPython operation goes through the interpreter: look up names, check types, allocate objects. On this board a hardware call costs about 40 µs; on real chips plain Python arithmetic also takes a few microseconds per operation, typically 10 to 100 times slower than C. (This course's board charges MicroPython time only for hardware calls and sleeps, so pure arithmetic looks free here; on a real chip it isn't.)
Size and memory
A C program contains only what it uses: a hundred bytes for a blinker, a few kilobytes for most jobs. MicroPython needs its interpreter in flash, at least about 256 KB, and about 16 KB of RAM at minimum, before any user code; Python objects then take more RAM than C's packed data. Small, cheap microcontrollers can run C but not MicroPython.
Timing
C code runs at a fixed speed, and its interrupts respond within a few cycles. MicroPython's speed varies with what the interpreter is doing; its garbage collector occasionally pauses to free memory; and its usual interrupts are soft, run between bytecodes. MicroPython can't give microsecond guarantees.
Writing it
MicroPython wins on the time it takes to write and change a program: no compile step, a prompt to try things on the live board, floats, strings and lists that just work, and errors reported with a line number instead of a crash.
Choosing, or mixing
| Use C for | Use MicroPython for |
|---|---|
| tight timing, fast interrupts | prototypes and experiments |
| small or cheap chips | logic that changes often |
| low power, battery life | text, data handling, user interfaces |
| drivers and signal processing | glue between finished components |
Many products mix them: C for the drivers and time-critical parts, exposed to MicroPython as a module, with the application written in Python on top.
Worked example
A sensor logger reads 5 registers and writes 10 bytes over the UART every second, and sleeps the rest of the time. How much awake time does each version need?
Show the solution
- Count the calls. About 15 hardware calls.
- MicroPython. plus 0.9 ms for the UART bytes at 115,200 baud.
- C. About 0.01 ms, plus the same 0.9 ms.
- Compare. Both are asleep more than 99.8% of the time, so here the language barely matters to the battery: the UART's time dominates.
Answer MicroPython needs 0.6 ms for the calls and C about 0.01 ms, plus 0.9 ms for the UART bytes either way.
Worked example
A motor commutation interrupt must respond within 2 µs. Which language?
Show the solution
- C. Its handler starts within a microsecond on this board.
- MicroPython. Its soft interrupts run only when the interpreter reaches a safe point, tens of microseconds or more later.
- Garbage collection. A collection can delay them by milliseconds.
Answer C, whose handler starts within a microsecond.
Worked example
Reading 1000 ADC samples takes one hardware call per sample. About how long does it take in MicroPython at 40 µs a call, and in C at about 10 instructions a sample?
Show the solution
- MicroPython.
- C. about 40 times faster.
- Does it matter? If the job happens once a second, either is fine; at 1000 times a second only C keeps up.
Answer about 40 ms in MicroPython and 1 ms in C.
Check
A MicroPython job makes 1000 hardware calls at 40 µs each. How long do they take?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
Check
A C loop makes 1000 hardware accesses at 5 cycles each, at 10 MHz. How long do they take?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
Check
A hardware access takes 40 µs in MicroPython and 7 cycles (100 ns each) in C. How many times faster is C?
Check
A chip has 256 KB of flash, and the MicroPython interpreter needs 256 KB. How much flash is left for everything else (negative if it doesn't fit)?
Check
A job runs 10 times a second, awake 0.5 ms per run in MicroPython or 0.05 ms in C, at 2 mA awake. How much more average current does the MicroPython version draw (sleep current aside)?
Write units the way you normally would: 1.5 mA, 1500 µA and 0.0015 A are the same answer.
Check
Which job is the best fit for MicroPython?
Check
Toggle P0 1000 times as fast as you can in C, then return.
C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.
Check
Measure, with time.ticks_us(), how long 50 toggles of P0 take in MicroPython, and print it.
MicroPython on the course's board: machine (Pin, PWM, ADC, DAC, UART, I2C, SPI, Timer, WDT) and time. Each call into the hardware takes about 40 µs. Tab indents; press Esc and then Tab to move on.
Check
A job makes 1000 hardware calls: about 40 µs each in MicroPython, or 10 instructions each in C on the 10 MHz board.
(a)
How long does it take in MicroPython?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
(b)
How many times faster is the C version?
Check
A sensor job runs 10 times a second and takes 1 ms in MicroPython.
(a)
What share of the time is MicroPython busy?
(b)
Does it fit at all (1 for yes)?
Explain why
Why do many products use C for drivers and timing-critical code but MicroPython (or another high-level language) for the application on top?
The two parts have different needs. Drivers and interrupt handlers run often, touch the hardware constantly and must meet microsecond deadlines, where C's speed and predictable timing are essential and its difficulty is worth paying once. The application logic (menus, data handling, protocols, configuration) changes far more often, runs less often, and is where most bugs and most of the development time go, so a language that's quicker to write, safer and easier to change saves the most there. Wrapping the C drivers as a module the high-level code calls gets each advantage where it counts, as long as the chip has room for the interpreter.
Practice
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.