how to convert code that uses cmp to python3
Ben Finney
ben+python at benfinney.id.au
Sat Apr 9 07:49:22 EDT 2016
Antoon Pardon <antoon.pardon at rece.vub.ac.be> writes:
> You don't seem to understand. I only do two comparisons and no the
> equality is not necesarrily cheaper.
>
> I am talking about the difference between the following two:
>
> if arg.key < node.key: # possible expensive computation
> go_left()
> elif arg.key == node.key # possible expensive computation
> found_node()
> else:
> go_right()
>
> and
>
> delta = cmp(arg.key, node.key) # possible expensive computation
> if delta < 0: # cheap computation
> go_left()
> elif delta == 0: # cheap computation
> found_node()
> else:
> go_right()
I find that a dubious claim.
The ‘cmp’ implementation must decide *at least* between three
conditions: less-than, equal-to, greater-than. That is *at least* two
inflection points.
The implementation of ‘__lt__’ and the implementation of ‘__eq__’ each
only need to decide two conditions (true, false). So that is *at most*
two inflection points.
If you're saying the latter implementation is somehow *more* expensive
than the former, I think the implementations are suspect and likely can
be improved: at least parity should be possible, to the point of
statistical insignificance.
--
\ “Perchance you who pronounce my sentence are in greater fear |
`\ than I who receive it.” —Giordano Bruno, burned at the stake by |
_o__) the Catholic church for the heresy of heliocentrism, 1600-02-16 |
Ben Finney
More information about the Python-list
mailing list