[Python-Dev] Python3 regret about deleting list.sort(cmp=...)

Fredrik Johansson fredrik.johansson at gmail.com
Sun Mar 13 00:59:20 CET 2011


On Sun, Mar 13, 2011 at 12:41 AM, "Martin v. Löwis" <martin at v.loewis.de> wrote:
> Am 12.03.11 18:00, schrieb Glenn Linderman:
>>
>>  On 3/12/2011 1:55 PM, Fredrik Johansson wrote:
>>>
>>> Consider sorting a list of pairs representing fractions. This can be
>>> done easily in Python 2.x with the comparison function lambda
>>> (p,q),(r,s): cmp(p*s, q*r). In Python 2.6, this is about 40 times
>>> faster than using fractions.Fraction as a key function.
>>
>> Am I correct in concluding that various ideas to eliminate or limit the
>> size of the key= cache would not help this use case at all?
>
> That's correct. However, there is a straight-forward day of getting
> the same comparison algorithm with the cmp_to_key class in 3.x.
> Fredrik classified this as "ugly and slow"; I'm not sure where this
> classification comes from.

It's ugly because it involves creating a class wrapping a comparison
function, or importing some obscure magic from the standard library,
instead of just using a comparison function.

It's slow because it's slow (and less memory-efficient). Even with
cmp_to_key, the example with fractions is still 2.6 times slower than
with a direct cmp function.

Fredrik


More information about the Python-Dev mailing list