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


Groups > comp.lang.python > #106725 > unrolled thread

Re: how to convert code that uses cmp to python3

Started byBen Finney <ben+python@benfinney.id.au>
First post2016-04-09 21:49 +1000
Last post2016-04-09 10:22 -0700
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  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

#106725 — Re: how to convert code that uses cmp to python3

FromBen Finney <ben+python@benfinney.id.au>
Date2016-04-09 21:49 +1000
SubjectRe: how to convert code that uses cmp to python3
Message-ID<mailman.115.1460202579.2253.python-list@python.org>
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

[toc] | [next] | [standalone]


#106748

FromPaul Rubin <no.email@nospam.invalid>
Date2016-04-09 10:22 -0700
Message-ID<87a8l2ofx1.fsf@nightsong.com>
In reply to#106725
Ben Finney <ben+python@benfinney.id.au> writes:
> The ‘cmp’ implementation must decide *at least* between three
> conditions...  The implementation of ‘__lt__’ and the implementation
> of ‘__eq__’ each only need to decide two conditions (true, false).

> If you're saying the latter implementation is somehow *more* expensive
> than the former, I think the implementations are suspect

What is suspect?  It seems natural to be able to distinguish the 3 cases
faster than two separate 2-case comparisons.  The most obvious example
is comparing long strings by scanning left to right.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web