Real-world Python code 700 times slower than C

Paul Hughett hughett at mercur.uphs.upenn.edu
Sat Jan 5 20:11:04 CET 2002


Skip Montanaro <skip at pobox.com> wrote:

:     Paul> Looking at the programs, I would suspect that most of the
:     Paul> difference is that Python has to allocate and free the array
:     Paul> 100000 times, but the C program doesn't.

: No, that's not the difference.  The Python code Brent posted only allocates
: the array once:

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


Actually, I meant to compare _my_ Python program (which has the
allocation hidden inside the call to pyvox.ramp) with the C program.
But it's still not the allocation; I modified the C program to malloc
and free "array" inside the loop calling Ramp, and it was not
noticeably slower.  So you were right, but for the wrong reason, and
I don't know why my Python program is so much slower than the C
program.  (My intuition is that computing the ramp and filling in the
array should constitute most of the execution time, and that is done
in C in both cases; but my intuition seems to be wrong here.)

Paul Hughett



More information about the Python-list mailing list