Python / C: small runtime difference?

Albert Hofkamp hat at
Fri Sep 12 14:08:06 CEST 2003

On Tue, 9 Sep 2003 15:54:41 +0200, Martin Schneider <martin.schneider at> wrote:
> Hi, Gerhard,
> thanks for your answer :-)
> Here's my python code:
> for i in range(len(fileList)-1):
>  for j in range(len(fileList)-i-1):
>   if fileList[j+1] < fileList[j]:
>    tmp = fileList[j]
>    fileList[j] = fileList[j+1]
>    fileList[j+1] = tmp
> Of course I don't know what Python does here internally.

You are modifying lists in-place, which means no copies of the data and
no copies of the lists are made, everything is done using pointer
copying (and updating reference counts).

>> It's best to have an array of pointers to structs. Then in
>> your bubble-sort algorithm, only exchange the pointers, don't copy the
>> structs themselves.
> Okay - I'll try that :-)

That should make a huge difference for anything but the simplest data.
Also, using a better sort algorithm should improve performance
dramatically (i.e. compare your code versus the builtin sort() function
of lists).

>> So if you make your C code as
>> smart as Python automatically is <wink>, your C code should be a hell of
>> a lot faster than your Python code :-)
> Maybe :-) I'll let you know.

It probably will not become slower :-)

Unlike popular belief, the .doc format is not an open publically available format.

More information about the Python-list mailing list