In the previous post I looked at the speed of the Pi for a particular floating point intensive simulation algorithm, coded in C. By running the code on the Pi and a couple of laptops I have available, this gave a useful indication of how fast the Pi hardware is relative to some other PCs. However, for languages other than C (and actually, even for C, to some extent), the speed of the code can depend as much on the implementation of the language (interpreter, compiler, byte-code compiler, virtual machine, etc.), as on the actual hardware. So in addition to knowing how fast your hardware is in theory, how fast your target language will run in practice is in fact the more relevant consideration. So, if python is your preferred language, knowing that the Pi should in principle be around the speed of a netbook is no good if the only python interpreter available for the Pi runs very slowly. In this post I’ll look at the speed of a simple python code running on the Pi and a couple of laptops I have.
In the previous post I demonstrated that of the currently available Debian-based distributions, only Raspbian has proper hard-float support and that this performs many times faster than other distros for floating-point intensive codes. I showed that a particular floating point intensive C code ran around at around half the speed of my netbook. Here we will look at the equivalent code in python. We will use the code “gibbs.py”:
import random,math def gibbs(N=50000,thin=1000): x=0 y=0 print "Iter x y" for i in range(N): for j in range(thin): x=random.gammavariate(3,1.0/(y*y+4)) y=random.gauss(1.0/(x+1),1.0/math.sqrt(2*x+2)) print i,x,y gibbs()
time python gibbs.py > /dev/null
However, on Raspbian, the code takes 150 minutes, 43 seconds. This is around 7 times slower than on my netbook. This is around 3 times slower than I would expect based on the C code. This suggests that the python interpreter for Raspbian is relatively inefficient.
For comparison purposes, this code runs in 3 minutes 52 seconds on my fast laptop. This is around 5 times faster than my netbook, which is roughly what I would expect based on the relative performance of the C code, which was around 6 times faster.
If anyone has any explanation for the relatively poor performance of python on Raspbian I’d be very interested to know…