Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #106725

Re: how to convert code that uses cmp to python3

From Ben Finney <ben+python@benfinney.id.au>
Newsgroups comp.lang.python
Subject Re: how to convert code that uses cmp to python3
Date 2016-04-09 21:49 +1000
Message-ID <mailman.115.1460202579.2253.python-list@python.org> (permalink)
References (7 earlier) <CAPTjJmrMv5O5vwrbfHJJCdiHi87naPOanhPmk4Ba_SLRgu1m4Q@mail.gmail.com> <5707BE18.1050805@rece.vub.ac.be> <CAPTjJmoMuqwg0Q_fp=qitC3wWkXYHtdqBNVGzO+3ToAmsgit8Q@mail.gmail.com> <5708C8B0.6050904@rece.vub.ac.be> <85twjb3su5.fsf@benfinney.id.au>

Show all headers | View raw


Antoon Pardon <antoon.pardon@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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: how to convert code that uses cmp to python3 Ben Finney <ben+python@benfinney.id.au> - 2016-04-09 21:49 +1000
  Re: how to convert code that uses cmp to python3 Paul Rubin <no.email@nospam.invalid> - 2016-04-09 10:22 -0700

csiph-web