Python speed

Context: I am a retired engineer making things to interest kids in electronics and programing. Nothing is for profit. After all Kids need to be prepared for a career and these skills are needed for small things like scanners and printers to EVs and factory machines that make things.

Since Eclipse left Fedora, I’ve been using Thonny to write Python code. I’m using Python to operate mechanisms of various kinds. via a Tkinter GUI The electronics that runs the motors and reads sensors in the mechanisms is interfaced via USB to a PC running Fedora Workstation. The USB chip in my electronics pretends to be an HID Serial device So I use the Python Serial library to “talk to” the electronics that runs the mechanisms. Though technically this is a real time application nothing I’ve done so far would be considered fast. For the projects I’ve done so far I’ve had >100 percent safety margins where responsiveness is concerned with my code running from Thonny or the command line. My modest requirements on responsiveness are due to simple motor controls which are run, direction, and speed. The first two are single bits and last is a three bit number all of which are sent in a single byte. So far there have only been three sensors all of them are single bit which are read in a single byte The actual changes for sensors is about 5 seconds between detections and a detection lasts lasts for about 2 seconds. The mechanisms work fine and reliably.

Lately I have been considering taking up some projects that would need to run faster (stepper motors). State changes for motor control would be between 10 and 20 times per second. I do intend to set up a bare USB interface of the kind I described earlier to test this, but I am mentally prepared for it to not work, but still hoping it may work to something like 10 per second.

Is the Python interpreter that is the standard interpreter installed with Workstation a line by line interpreter or does it do any AOT or JIT?

I have a fall back for the faster needs. I can make the electronics more complicated, but I’d prefer not to do that. Simple is more reliable and easier to understand.

Any guidance or suggestions in regard to my probable desire for more speed in python will be appreciated.

Thank you in advance.

1 Like

The answer probably depends on which interpreter you are using.

Explanation taken from this Real Python article (you’ll find lots more info to Python performance there):

The Python language specification is used in a number of implementations such as CPython (written in C), Jython (written in Java), IronPython (written for .NET), and PyPy (written in Python).

CPython is the original implementation of Python and is by far the most popular and most maintained. When people refer to Python, they more often than not mean CPython. You’re probably using CPython right now!

I would recommend starting with the default (CPython) and only try PyPy if you see performance issues.

Edit: According to this article, the next python (CPython) version (3.13) will have a JIT compiler. I guess that means that previous versions don’t.

Python compiles into byte code and runs the byte code.
There are optimisation applied, and more coming in future versions.

I suggest that you try using python and if you run into problems ask for help on https://discuss.python.org/
Doing things 10 times a second does not seem stressful for python.

Added python