Python / C: small runtime difference?

Gerhard Häring gh at ghaering.de
Tue Sep 9 13:27:16 CEST 2003


Martin Schneider wrote:
> One step ahead: My test program sorted a two-dimensional array containing
> pairs of a string and a long integer, and the strcpy routine seems pretty
> slow.
> 
> After reducing the problem to numerical sort only, I could improve the
> relation to 1:50.
> 
> Is this a normal ratio?

Not for optimized C. But your algorithm is very naive I must say. If I 
were to implement such an algorithm in C, I'd avoid copying as much as 
possible. 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.

FWIW your Python code most likely doesn't copy either, it only moves 
references around, which is quite fast. 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 :-)

HTH,

-- Gerhard





More information about the Python-list mailing list