Forum Moderator
Forum Moderator
Posts: 4991
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Running compiled code

Sun Jun 10, 2012 7:14 am

I have written a python program that writes to a LCD module via the gpio port. The program appears to run quite slowly, noticeably slower than equivalent C code, but I expect you get that. I notice in Geany that you can create a compiled version of the program with the filename ending in a .pyc extention. There appears to be no advantage in running this file speedwise - its no faster, and also I still need to use the same command structure to run it, ie

Code: Select all

pi@raspberry # sudo python gpio.pyc
In fact it has less options to run because I can

Code: Select all

pi@raspberry # sudo ./gpio.py
but I cannot

Code: Select all

pi@raspberry # sudo ./gpio.pyc
So either I,m missing something, or 'thats what you get!'.
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):

Posts: 9
Joined: Sun Jun 10, 2012 7:47 am

Re: Running compiled code

Sun Jun 10, 2012 7:56 am

".pyc" files contain python bytecode. When you run python source code, it gets compiled to bytecode for the interpreter to run (this always happens - it's how python works). You can run bytecode files without the source-code but they do not run any faster. The python bytecode compilation only speeds up the time to load the code into memory.

If you want your application to run faster, take a look at Cython (www.cython.org). This compiles python code to a native library which python can load. You start with pure python then add C type declarations to get speed-ups. Note, however: even if you make your own code fast with Cython, the calls to the GPIO library will be a bottleneck. To get real speed improvements, you should re-write the GPIO library using Cython to avoid calls using the python runtime. Luckily, the GPIO library is rather trivial (140 lines of python, all in) so rewriting this with Cython should take too long.


BTW. I found I needed a swap-file active in order to compile Cython on my RPi, otherwise it ran out of memory during the build process.

Posts: 265
Joined: Tue Jan 10, 2012 11:05 am

Re: Running compiled code

Sun Jun 10, 2012 9:16 am

Performance when running will not be better, overall performance will be affected as creating a pyc file will improve startup time. It only really misses out the byte-compile phase - which for a small program is negligible anyway.

You should be able to run the .pyc directly.
Firstly check the permissions on the pyc file allow you to run it.
Then if that is the case there is a small possibility than the right binfmt_misc entry doesn't exist in the /proc/sys/fs/binfmt_misc directory. I think this is unlikely for an out of the box python - but if you have compiled up a version of python yourself it might be the case. I haven't got my raspi to hand so I can't test this myself at the moment.

Posts: 265
Joined: Tue Jan 10, 2012 11:05 am

Re: Running compiled code

Mon Jun 11, 2012 10:50 am

Right - after reorganising my home office I now have everything running - two PCs (one of which is a server), two laptops, two printers, the pi, a NAS, various routers and hubs etc.

It seems that the debian install is a very minimal install. binfmt_misc isn't mounted and knows nothing about any magic files.

However all is not lost.


Code: Select all

sudo apt-get install binfmt_support
sudo /etc/init.d/binfmt_support start
Your raspi should now know how to run Python 2.6 .pyc files.

If you have binfmt_support installed, installing new packages (java, Python 3, etc) should update the relevant files.

Return to “Python”

Who is online

Users browsing this forum: asandford and 27 guests