13

I've been benchmarking the Pi on some of my simulation codes, relative to a couple of laptops I have. My codes tend to be floating point intensive, so I've been using Raspbian (which turns out to be much faster) due to its hard-float support. I have the same simple code in several different languages. Obviously python code is slower than C code whatever platform I use, but on the Pi it appears to be relatively more slow, by a factor of around 3. Does anyone know why the python interpreter is likely to be relatively slow on the Pi, and is this something that is likely to be fixed?

Here is the test:

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()

From this blog post about the experiment.

Darren Wilkinson
  • 2,912
  • 4
  • 26
  • 27
  • 2
    Self promotion is fine (within reason) but all your blog link really added to the question was the code, so I've moved it here instead. – Jivings Jul 02 '12 at 20:05
  • 2
    Um, it also contained details and timings and other potentially useful background which substantiated the claim that python is slower relative to C on the Pi than on Intel based linux machines, which was kind of the point of the question... – Darren Wilkinson Jul 02 '12 at 20:18
  • 2
    I read the whole post, I don't think it contributed information incredibly relevant to the question. And asking everyone to read a page of information before they can answer your question is not going to get you a lot of answers. Programmers are by definition, lazy. We need all the information in easy to digest chunks :) – Jivings Jul 02 '12 at 20:24
  • 8
    That's a judgement call that I'm fine with, as I'm an easy-going kind of guy... But I am a bit concerned that this zealous over-editing of almost every question posted on this site is likely to deter people from participating. I know it's done with the best of intentions, but you know what they say about the road to Hell... I really think it's something that all of you very active question editors should take some time to think and chat about. It would be a real shame if this site didn't take off because of the actions of a few well-intentioned but misguided individuals. – Darren Wilkinson Jul 02 '12 at 20:48
  • You bring up interesting points, please come and discuss this issue in chat if you have the time. – Jivings Jul 02 '12 at 21:04
  • I can't tell you why it's slower, but you should use numpy for that and it will be nearly as fast as doing it in C. – Alistair Buxton Jul 23 '12 at 07:09
  • @AlistairBuxton numpy is great, but this kind of iterative algorithm does not vectorise nicely, so numpy doesn't really help here. – Darren Wilkinson Jul 23 '12 at 13:12
  • For information, this problem doesn't seem to be specific to python. It is also true for R, another high level language. It just seems that the speed difference between C and higher level languages is greater on the Pi than on Intel. There could be fundamental reasons why this is the case, I'm just not clear what they are. – Darren Wilkinson Jul 23 '12 at 13:14
  • 1
    Stupid question... were you running the same version of python on both sides of the equation? I would expect on the same hardware, python3 will run the same code faster than python2 because range on python3 is an iterator and on python2, it creates the list which is less efficient (use xrange on python2)... see for example http://stackoverflow.com/questions/94935/what-is-the-difference-between-range-and-xrange – Foon Apr 03 '13 at 01:07
  • 1.) Your blog post link is down- *exactly* the reason we edit posts with external code. If an edit hadn't been made, this post wouldn't have been useful. – Anonymous Penguin Feb 13 '15 at 22:08
  • 2.) I would like to see the C code you're comparing to and see if compiler optimizations are causing this issue. – Anonymous Penguin Feb 13 '15 at 22:09
  • Broken link now fixed. – Darren Wilkinson Feb 15 '15 at 15:42

2 Answers2

7

I would guess that the Python interpreter is simply not optimized for ARM. Python might have been optimized for the other platforms. In my experience, this is true for software like OpenSSH, so I assume it's similar for Python.

Kevin Chen
  • 186
  • 4
2

Python probably suffers from lack of cpu cache. I'm not sure how you can measure that easily though

John La Rooy
  • 11,947
  • 9
  • 47
  • 75