Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #106733
| Path | csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: how to convert code that uses cmp to python3 |
| Date | Sun, 10 Apr 2016 00:41:16 +1000 |
| Lines | 50 |
| Message-ID | <mailman.119.1460212879.2253.python-list@python.org> (permalink) |
| References | <57064D0D.1030701@rece.vub.ac.be> <CAPTjJmrGiQS9uhFU-+TQ4nUWQ2p4gmJoQitMotS0Ob--qm1iWg@mail.gmail.com> <5706C961.2000009@rece.vub.ac.be> <CAPTjJmrbL870mV1kU8nHar=bPyKZRmhKP-8iUs_tpYVo5vhhOA@mail.gmail.com> <57075F43.7060004@rece.vub.ac.be> <85fuuw5ypl.fsf@benfinney.id.au> <5707B2CE.1010407@rece.vub.ac.be> <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> <570910E2.3030400@rece.vub.ac.be> <CAPTjJmqeWACzrzka+5kQxO9Co42k7jRJzG_+cCqHkYO2OtQtMw@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de 1hZ4p3HPmI8tZPkGVOqXVgMAluNWiof+EORk+ghHJHVw== |
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'elements.': 0.05; 'subject:code': 0.07; 'cc:addr:python-list': 0.09; 'absent': 0.09; 'definition,': 0.09; 'dict': 0.09; 'happen.': 0.09; 'integers': 0.09; 'lengths': 0.09; 'lookup': 0.09; 'timestamp': 0.09; 'times,': 0.13; 'def': 0.13; 'argument': 0.15; '*no*': 0.16; '2016': 0.16; '__lt__': 0.16; 'bucket': 0.16; 'cmp': 0.16; 'dictionary.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'insertions': 0.16; 'iterables': 0.16; 'keys.': 0.16; 'lookups': 0.16; 'naive': 0.16; 'nodes': 0.16; 'pathological': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:python3': 0.16; 'traverse': 0.16; 'twice.': 0.16; 'wrote:': 0.16; 'implementing': 0.18; 'tree': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'delta': 0.22; 'function,': 0.22; 'function:': 0.22; 'keys': 0.22; 'tuples': 0.22; 'pass': 0.22; 'am,': 0.23; "python's": 0.23; 'header:In-Reply-To:1': 0.24; 'example': 0.26; 'message-id:@mail.gmail.com': 0.27; '(it': 0.29; 'branches': 0.29; 'comparison': 0.29; 'hash': 0.29; 'once.': 0.29; 'subject:that': 0.29; "i'm": 0.30; 'operations': 0.31; 'probably': 0.31; "can't": 0.32; 'maybe': 0.33; 'problem': 0.33; 'common': 0.33; 'largely': 0.33; 'case,': 0.34; 'equal': 0.34; 'structure': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'something': 0.35; 'but': 0.36; 'received:209.85': 0.36; '(and': 0.36; 'totally': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'turn': 0.37; 'missing': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'end': 0.39; 'means': 0.39; 'data': 0.39; 'takes': 0.39; 'ever': 0.60; 'your': 0.60; 'close': 0.61; 'decision': 0.61; 'show': 0.62; 'making': 0.62; 'more': 0.63; 'different': 0.63; 'account': 0.66; 'fact,': 0.67; 'situation': 0.67; 'chrisa': 0.84; 'pardon': 0.84; 'probable,': 0.84; 'tree,': 0.84; 'visit,': 0.84; 'to:none': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc; bh=Y2WSihtww81yK4LdYNG1WMBCszXXIh2udt5V4GQfTAc=; b=ccn98OH9A4h8y4Y8zYssXSJszzYZjlCZGs+ox7USUD+bS0hE3zMUNWqWeh+nyF8bkw nBfvjg13+FllfjJy7mxM8+K90PVVbF7wqBFQcYCB3R0hmKkZzrUvJb4mn/bljbIQRTfP QEDPN5x+OhLsqaTXpbrh2iBQbedatkEP6wm1hdnzuEdJPsBgce/CDp5GiZ3zrnJ0ql44 4QauClz+V13uaSLxTO2BBjIsk3s74Qu5dyIV8DmRHe9HROPoLStsbR/HfbOhva4GyGgY odBxV3aK5Tv1aY8NgSKDYUh8F/B3TPvFIJP8/ONHdjNscdrLNVByH4bVu3irMuoLAy2H XoHQ== |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=Y2WSihtww81yK4LdYNG1WMBCszXXIh2udt5V4GQfTAc=; b=KPH+qcBvrNFArB9m4oyXe9D9f2rQ6aUJLbgHEaNC0ZUWXeRi1smPVSEhWlK2udhukI aVwSC4g8WwouoqYgEMqNWvXh1g4fqGue6CGQK2rA+6ooFETc3867bjMiDNmRmF4DO486 SEbtw3zOL1O2A3bJ8qfQqS0u1fGxslNUwlI6kt5+5S8mdqgqDTlQRye1GkAUyCe2CpAX yiLHnoRC/KWW51ll7zmcZbla6D/0uMkYUnLSP5iNnBff9A7wbxXCuiv+4VmOp6jYj/eH mIc5OIdKpI7jWV5hWg/hmCma16luzw6CBjkFBIWIQqHuUNOQLyoEbDv3bEAuMTZeyxgt f8Xw== |
| X-Gm-Message-State | AD7BkJJB899uo9GGtze3A1aeK/vsiKrjyv6nPhmskM19kbDzMCKidWFYqHTBzrPKiR1xhtDktIZVPtIx+RnuLQ== |
| X-Received | by 10.50.112.169 with SMTP id ir9mr9594105igb.92.1460212876722; Sat, 09 Apr 2016 07:41:16 -0700 (PDT) |
| In-Reply-To | <570910E2.3030400@rece.vub.ac.be> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.21 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| X-Mailman-Original-Message-ID | <CAPTjJmqeWACzrzka+5kQxO9Co42k7jRJzG_+cCqHkYO2OtQtMw@mail.gmail.com> |
| X-Mailman-Original-References | <57064D0D.1030701@rece.vub.ac.be> <CAPTjJmrGiQS9uhFU-+TQ4nUWQ2p4gmJoQitMotS0Ob--qm1iWg@mail.gmail.com> <5706C961.2000009@rece.vub.ac.be> <CAPTjJmrbL870mV1kU8nHar=bPyKZRmhKP-8iUs_tpYVo5vhhOA@mail.gmail.com> <57075F43.7060004@rece.vub.ac.be> <85fuuw5ypl.fsf@benfinney.id.au> <5707B2CE.1010407@rece.vub.ac.be> <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> <570910E2.3030400@rece.vub.ac.be> |
| Xref | csiph.com comp.lang.python:106733 |
Show key headers only | View raw
On Sun, Apr 10, 2016 at 12:25 AM, Antoon Pardon <antoon.pardon@rece.vub.ac.be> wrote: > Let me give you an artifical example to show what can happen. The keys are all > iterables of equal lengths with integers as elements. > > Then this could be the cmp function: > > def cmp(ob1, ob2): > itr1 = iter(ob1) > itr2 = iter(ob2) > for el1, el2 in zip(itr1, itr2) > delta = el1 - el2 > if delta != 0: > return delta > return 0 > > Now maybe I'm missing something but I don't see how implementing __lt__ and > __eq__ in this case will be much different from largely reimplenting cmp. > So if you can work with a cmp function, you only need to travese the two > iterables only once. If you do it with the __lt__ and __eq__ methods you > will have to traverse them twice. > > Now this probably is not a problem most of the times, but when you work with > tree's this kind of comparison to make a three way decision happens often and > the lower you descend in the tree, the close your argument will be with the > keys of the nodes you visit, making it more and more probable, you have a > large common prefix. > > So in these circumstances it is more likely to be problematic. In this case, you're likely to end up with large branches of your tree that have the same prefix. (And if you don't, your iterations are all going to end early anyway, so the comparison is cheap.) A data structure that takes this into account will out-perform the naive comparison model every time. In fact, a simple dict will probably out-perform your tree; by definition, your iterables have to be stable, which means you could simply turn them into tuples and use them as dict keys. Lookups and insertions would both require one pass over the current key to calculate its hash, then a bucket lookup; absent a pathological situation with hash collisions, this will result in FAR better performance than any tree lookup ever will. The advantage of the tree is that it requires *no* operations other than "which of these is greater". But I'm hard-pressed to find an object type that would plausibly be used in this way (it has to be totally ordered, for a start - you can't use timestamp ranges, for instance), yet can't take advantage of Python's massively-optimized dictionary. ChrisA
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: how to convert code that uses cmp to python3 Chris Angelico <rosuav@gmail.com> - 2016-04-10 00:41 +1000
csiph-web