Sorting: too different times. Why?

Chris Rebert clp2 at
Sun Nov 22 12:07:08 CET 2009

On Sun, Nov 22, 2009 at 2:56 AM, n00m <n00m at> wrote:
> I was expecting the 1st method would be *slower* than the 2nd one :-)
> Or at least equal... Just random ("intuitive") expectations

The second method repeatedly looks up left_item.__class__.__cmp__
(i.e. Vector.__cmp__) when doing the necessary comparisons between the
list items; while these lookups are optimized and are fast, they are
not free and cannot be skipped because Python doesn't know the list
contains only Vectors.
The first method uses the single provided comparison function and thus
does no such lookups; hence, it's faster.

That's my guess anyway.


More information about the Python-list mailing list