For an index to all my stories click this text
In the previous story I wrote how you can make your MicroPython sourcecode unreadable for most users. I did that for trying to protect my program. The way to do this is to convert your program into bytecode. You can read that story here:
https://lucstechblog.blogspot.com/2025/06/protect-your-micropython-sourcecode.html
After I published that story here on my weblog I received a mail from a reader that asked if the bytecode was actually faster as standard MicroPython. The question is valid as bytecode is actually easier to interpret as MicroPython code.
So I did some tests.
Prime numbers
I took a smal program that calculated and printed a few thousand prime numbers. Here is the code.
import time start_time = time.ticks_ms() def is_prime(n): if n < 2: return False for i in range(2, int(n ** 0.5) + 1): if n % i == 0: return False return True count = 0 n = 2 while count < 5000: if is_prime(n): print(n) count += 1 n += 1 end_time = time.ticks_ms() elapsed_time = time.ticks_diff(end_time, start_time) print("Elapsed time:", elapsed_time, "milliseconds")
This code took 32993 miliseconds to run on an ESP8266 with MicroPython V1.20.1.
Then I converted the program to bytecode and it took 32149 microseconds to run. So it was 844 miliseconds (almost a full second) faster.
That is 2.5% faster.
So it is a bit faster but not a lot to gain..
Without print
A lot of time is consumed by the print statement. Print is dependend on the serial communication between the microcontroller and your computer. So let's see what we gain if we leave the print command out.
import time start_time = time.ticks_ms() def is_prime(n): if n < 2: return False for i in range(2, int(n ** 0.5) + 1): if n % i == 0: return False return True count = 0 n = 2 while count < 5000: if is_prime(n): count += 1 n += 1 end_time = time.ticks_ms() elapsed_time = time.ticks_diff(end_time, start_time) print("Elapsed time:", elapsed_time, "milliseconds")
It's the same program without the print command in the while loop.
The program now took 32385 miliseconds to run.
Again I converted it to bytecode and then it needed 31570 miliseconds to run.
That is 815 miliseconds faster. Again almost a second faster.
And that makes it also 2.5% faster.
This is just a small test but this already shows that bytecode is a bit faster as MicroPython code. So you might gain some speed in certain projects.
Conclusion
Byte code is not real native machine language but an in-between step. This small test already shows that bytecode is a bit faster as MicroPython code. So you might gain some speed in certain projects.
This is not a scientific test just a small
test to see if there is any speed to gain. Your results might vary
depending on the Microcontroller you are using (ESP8266, ESP32,
Raspberry Pi pico etc.) size of the program, libraries used and commands
used. So make some tests for yourself.
Till next time,
have fun
Luc Volders