Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'compiler': 0.05; 'cpython': 0.05; 'character,': 0.07; 'strings.': 0.07; 'scripts': 0.09; 'python': 0.09; 'builtins': 0.09; 'pointers': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sep': 0.09; 'looked': 0.10; '"this': 0.13; 'language': 0.14; 'benjamin': 0.16; 'equal.': 0.16; 'literals': 0.16; 'not;': 0.16; 'python),': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'recognizing': 0.16; 'unequal': 0.16; 'string': 0.17; 'wrote:': 0.17; 'differ': 0.17; 'instance,': 0.17; 'pointer': 0.17; '>>>': 0.18; 'module': 0.19; 'otherwise,': 0.20; 'bit': 0.21; 'struct': 0.22; "haven't": 0.23; 'idea': 0.24; 'header:User-Agent:1': 0.26; 'am,': 0.27; '(as': 0.27; '(such': 0.27; 'header:X-Complaints- To:1': 0.28; 'chris': 0.28; 'decide': 0.28; '>>>>': 0.29; 'character.': 0.29; 'comparison': 0.29; "d'aprano": 0.29; 'implies': 0.29; 'inspect': 0.29; 'optional': 0.29; 'steven': 0.29; 'source': 0.29; 'probably': 0.29; 'this.': 0.29; 'url:python': 0.32; 'could': 0.32; '11,': 0.33; 'to:addr:python- list': 0.33; 'likely': 0.33; 'that,': 0.34; 'version': 0.34; 'subject:?': 0.35; 'there': 0.35; 'received:org': 0.36; 'really': 0.36; 'but': 0.36; 'url:org': 0.36; 'compare': 0.36; 'depends': 0.36; 'charset:us-ascii': 0.36; 'optimization': 0.37; 'does': 0.37; 'quite': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'skip:u 10': 0.60; 'most': 0.61; 'here:': 0.62; 'needing': 0.62; 'received:ac.uk': 0.65; 'guaranteed': 0.76; 'gain': 0.79; 'oscar': 0.84; 'url:cpython': 0.84; 'url:include': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Oscar Benjamin Subject: Re: Comparing strings from the back? Date: Mon, 10 Sep 2012 14:43:34 +0000 (UTC) References: <504564ba$0$29978$c3e8da3$5496439d@news.astraweb.com> <504761ef$0$29981$c3e8da3$5496439d@news.astraweb.com> <50477cbb$0$29981$c3e8da3$5496439d@news.astraweb.com> <50485fca$0$29977$c3e8da3$5496439d@news.astraweb.com> <504972d1$0$29981$c3e8da3$5496439d@news.astraweb.com> <504deedc$0$29981$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: enm-ob.enm.bris.ac.uk User-Agent: slrn/0.9.9p1/mm/ao (Win32) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1347288231 news.xs4all.nl 6852 [2001:888:2000:d::a6]:33053 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:28823 On 2012-09-10, Oscar Benjamin wrote: > On 2012-09-10, Chris Angelico wrote: >> On Tue, Sep 11, 2012 at 12:06 AM, Oscar Benjamin >> wrote: >>> On 2012-09-10, Steven D'Aprano wrote: >>>> What interning buys you is that "s == t" is an O(1) pointer compare if >>>> they are equal. But if s and t differ in the last character, __eq__ will >>>> still inspect every character. There is no way to tell Python "all >>>> strings are interned, if s is not t then s != t as well". >>>> >>> >>> I thought that if *both* strings were interned then a pointer comparison >>> could decide if they were unequal without needing to check the characters. >>> >>> Have I misunderstood how intern() works? >> >> In a language where _all_ strings are guaranteed to be interned (such >> as Lua, I think), you do indeed gain this. Pointer inequality implies >> string inequality. But when interning is optional (as in Python), you >> cannot depend on that, unless there's some way of recognizing interned >> strings. Of course, that may indeed be the case; a simple bit flag >> "this string has been interned" would suffice, and if both strings are >> interned AND their pointers differ, THEN you can be sure the strings >> differ. >> >> I have no idea whether or not CPython version X.Y.Z does this. The >> value of such an optimization really depends on how likely strings are >> to be interned; for instance, if the compiler automatically interns >> all the names of builtins, this could be quite beneficial. Otherwise, >> probably not; most Python scripts don't bother interning anything. >> > > I haven't looked at the source but my understanding was precisely that there > is an intern() bit and that not only the builtins module but all the literals > in any byte-compiled module are interned. > s/literals/identifiers/ You can see the interned flag in the PyUnicodeObject struct here: http://hg.python.org/cpython/file/3ffd6ad93fe4/Include/unicodeobject.h#l303 Oscar