Python is faster than C
Duncan Booth
me at privacy.net
Sun Apr 4 07:43:28 EDT 2004
Joe Mason <joe at notcharles.ca> wrote in
news:slrnc6ud7b.4u7.joe at gate.notcharles.ca:
> In article <406F0907.96F37EA1 at tunes.org>, Armin Rigo wrote:
>> The reason that Psyco manages to outperform the C implementation is not
>> that gcc is a bad compiler (it is about 10 times better than Psyco's).
>> The reason is that the C implementation must use a generic '<' operator
>> to compare elements, while the Psyco version quickly figures out that it
>> can expect to find ints in the list; it still has to check this
>> assumption, but this is cheap and then the comparison is done with a
>> single machine instruction.
>
> Why can't the C implementation do the same thing?
>
It easily could, however sorting a list of ints sounds to me like a
comparatively unusual thing to do in any real application. Sorting strings,
or sorting more complex data structures are much more usual. In other words
it would be kind of pointless to introduce an optimisation that only helps
speedup benchmarks.
Also the builtin sort handles other cases which are important. Not all data
you want to sort is randomly ordered. The builtin sort method should
outperform quicksort in those cases where the input is already largely
sorted. The builtin sort is also stable, although for a list of random
integers you might be hard pushed to tell :-)
More information about the Python-list
mailing list