Guido rethinking removal of cmp from sort method

Duncan Booth duncan.booth at invalid.invalid
Mon Mar 28 11:07:39 EDT 2011


Antoon Pardon <Antoon.Pardon at rece.vub.ac.be> wrote:

> I tried to sort lists of 10000 elemens. Each element a tuple two items
> that was to be sorted first according to the first item in ascending
> order, then according to the second item in descending order. This was
> done on python 2.6 so I had to write my own cmp_to_key function.
> 
> I then sorted these lists of 10000 elements a 1000 times with the
> following methods.
> 
> 1) a cmp-function
> 2) key = cmp_to_key
> 3) key = wrapper class around cmp-function
> 4) key = class specifically written to implement the ordering.
> 
> This was done, once with number tuples and and once with string
> tuples. The result in seconds
> 
>   numbers   strings
> 
> 1   63.8      74.3
> 2  155.3     169.6
> 3  156.1     170.2
> 4   96.2     104.4
> 
> Now you can of course ignore those numbers because they don't come
> from a real world example and thus consider such numbers unimportant
> until someone in the future has a case similar enough with the above
> and wonders why sorting his data is so slow.
> 

Out of interest, what happens to your timings if you do the sort just 
using simple key functions?

Since you've got two keys you will of course need to make two calls to 
'sort'. e.g.

    data.sort(key=itemgetter(1), reverse=True)
    data.sort(key=itemgetter(0))

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list