Native & Viper Emitters: Direct Machine Code Boosts
For critical performance, MicroPython features 'native' and 'viper' code emitters that compile Python functions directly into machine-specific CPU opcodes. The native emitter approximately doubles speed, while the Viper emitter offers even greater gains, especially for integer arithmetic, by using specialized native data types.
The top 3
- Native vs. Viper: Top Speed Boosts Compared: MicroPython's Native emitter typically doubles bytecode execution speed, while the Viper emitter can deliver significantly higher gains, often 10-16x or even near-C speeds, particularly for integer arithmetic and array operations.
- Viper's C-Speed Secret: Raw Types & Pointers: The Viper emitter achieves near-C performance by supporting non-standard raw data types like `int`, `uint`, `ptr`, and direct memory access (pointers), bypassing Python object overhead for highly optimized integer arithmetic and bit manipulations.
- Emitter's Sweet Spot: Code Patterns for Max Speed: Native and Viper emitters excel in CPU-bound tasks, particularly those involving intensive integer arithmetic, bit manipulation, and direct array access. For instance, a function performing array additions with Viper can be 16 times faster on an ESP32-S3, while a prime number test saw a 108.8x speedup. They are less effective for floating-point operations, dictionary lookups, or code with frequent object allocations.
Sources
Open the full topic