Guido rethinking removal of cmp from sort method

Aahz aahz at pythoncraft.com
Fri Apr 8 21:40:18 CEST 2011


In article <4d9f32a2$1 at dnews.tpgi.com.au>,
Lie Ryan  <lie.1296 at gmail.com> wrote:
>On 04/09/11 01:08, Aahz wrote:
>>
>> Actually, my take is that removing __cmp__ was a mistake.  (I already
>> argued about it back in python-dev before it happened, and I see little
>> point rehashing it.  My reason is strictly efficiency grounds: when
>> comparisons are expensive -- such as Decimal object -- __cmp__ is
>> faster.)
>
>I don't get you... why would sorting a list using __cmp__ be faster when
>comparisons are expensive?

Not sorting (because sorting only requires one comparison), but any
operation involving multiple comparisons.  Consider this:

if a < b:
    x()
elif a == b:
    y()
else:
    z()

For a >= b, you need to make two comparisons.  Now consider this:

r = cmp(a, b)
if r < 0:
    x()
elif r == 0:
    y()
else:
    z()
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"At Resolver we've found it useful to short-circuit any doubt and just        
refer to comments in code as 'lies'. :-)"
--Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22



More information about the Python-list mailing list