Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsfeed.xs4all.nl!newsfeed6.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.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'extent': 0.07; 'builtins': 0.09; 'pointers': 0.09; 'sep': 0.09; 'looked': 0.10; 'ah,': 0.16; 'benjamin': 0.16; 'earlier.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'literals': 0.16; 'wrote:': 0.17; 'unicode': 0.17; 'module': 0.19; 'bit': 0.21; 'received:209.85.214.174': 0.21; 'struct': 0.22; "haven't": 0.23; 'seems': 0.23; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'there.': 0.28; 'comparison': 0.29; 'case,': 0.29; 'source': 0.29; 'function': 0.30; 'url:python': 0.32; 'could': 0.32; '11,': 0.33; 'to:addr:python- list': 0.33; 'received:google.com': 0.34; 'or,': 0.34; 'subject:?': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'url:org': 0.36; 'unable': 0.36; 'possible': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'skip:u 10': 0.60; 'here:': 0.62; 'different': 0.63; 'oscar': 0.84; 'url:cpython': 0.84; 'url:include': 0.84 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:to :content-type; bh=4Z3brMEbwoKP1B3wV9FE3Fh1u8nT5JEG7Mk7OB2DALc=; b=wtHTt7OTIkEzfRhCCABaVVUKtOZ5tT8GpnOYtAZBEkCxBwVR27QHfoihUcaoXFLxYN eDhz/S7dmKOVQXzkgdbgKKNHke+4vW2gvvr1Q2l59iDn78AQe0wo+dEaM/aAzLBlSsrq 4ZFEg6F6Lj8YhVbzMYXs0AJbtkf0OiOtdFEgTtG9Yif6LUnbuaq2gZ611o4jerrIpwgC Xu4mLri1eP12tKEy035G/5nqcCCPmXbtqqPd8YfvuIZwlCFLEoioXWhA+oZ8VJGojHKu xSgnEUY3dBFVJ7+G+AMCYKvI3qMYbGE/0rTc3KycNjogviwmWUsZ/ormutC9iEwhSWq6 0hSQ== MIME-Version: 1.0 In-Reply-To: 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> Date: Tue, 11 Sep 2012 00:56:09 +1000 Subject: Re: Comparing strings from the back? From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1347288972 news.xs4all.nl 6859 [2001:888:2000:d::a6]:36349 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:28825 On Tue, Sep 11, 2012 at 12:43 AM, Oscar Benjamin wrote: > On 2012-09-10, Oscar Benjamin wrote: >> 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 Ah, yep, so that's there. In that case, it's possible to have that optimization. However, I may be misreading this, but it seems the only Unicode comparison function is a rich compare, which is unable to take advantage of a known difference: http://hg.python.org/cpython/file/b48ef168d8c5/Objects/unicodeobject.c#l6114 Different pointers prove the strings differ, but don't tell you which is to be sorted earlier. You could use this if you roll your own comparison in C; or, if you already know the strings are interned, you can use 'is' / 'is not'. But that seems to be the extent of it. ChrisA