Real-world Python code 700 times slower than C

Paul Hughett hughett at mercur.uphs.upenn.edu
Sat Jan 5 13:13:21 EST 2002


Brent Burley <brent.burley at disney.com> wrote:

: I often use a "10x" rule of thumb for comparing Python to C, but I
: recently hit one real-world case where Python is almost 700 times
: slower than C!  We just rewrote the routine in C and moved on, but
: this has interesting implications for Python optimization efforts.

: python
: ------
: def Ramp(result, size, start, end):
:     step = (end-start)/(size-1)
:     for i in xrange(size):
:         result[i] = start + step*i

: def main():
:     array = [0]*10000
:     for i in xrange(100):
:         Ramp(array, 10000, 0.0, 1.0)

: main()


I rewrote your Python fragment using my pyvox extension (which is designed
for efficient processing of volume images) to get

import pyvox, exim

def Ramp(size, start, end) :
    step = float((end-start) / (size-1))
    return step * pyvox.ramp(size, exim.double)

def main() :
    for i in xrange(100000) :
        array = Ramp(10000, 0.0, 1.0)

main()


This program runs in 45.4 sec on my machine, vs 16.0 for the C version
you gave (with gcc -O2 optimization); that cuts the performance ratio
down to only 2.8:1.  Looking at the programs, I would suspect that
most of the difference is that Python has to allocate and free the array
100000 times, but the C program doesn't.

If you want to look into pyvox, go to

http://www.uphs.upenn.edu/bbl/bblimage

It's part of the bblimage package.


Paul Hughett



More information about the Python-list mailing list