Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'from:addr:yahoo.co.uk': 0.04; 'string.': 0.05; 'subject:Python': 0.06; 'correct.': 0.07; 'character,': 0.09; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'width': 0.09; 'def': 0.12; 'posted': 0.15; 'be:': 0.16; 'incorrect': 0.16; 'mean,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'roy': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'thu,': 0.19; 'later': 0.20; '>>>': 0.22; 'tests': 0.22; 'header:User-Agent:1': 0.23; 'string,': 0.24; 'url:moin': 0.24; 'this:': 0.26; 'subject:/': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'patch': 0.29; 'characters': 0.30; 'code': 0.31; 'url:wiki': 0.31; '>>>>': 0.31; "d'aprano": 0.31; 'inspect': 0.31; 'steven': 0.31; 'url:python': 0.33; "can't": 0.35; 'there': 0.35; 'done': 0.36; 'url:org': 0.36; 'unit': 0.37; 'to:addr:python-list': 0.38; 'little': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'read': 0.60; 'ian': 0.60; 'entire': 0.61; "you're": 0.61; 'first': 0.61; 'more': 0.64; 'charset:windows-1252': 0.65; 'here': 0.66; 'smith': 0.68; 'safe': 0.72; 'quality': 0.72; 'sir,': 0.81; '9:02': 0.84; 'subject:long': 0.84; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: Performance of int/long in Python 3 Date: Wed, 03 Apr 2013 23:39:19 +0100 References: <87dff083-14d8-4163-89f3-d78a9be6c802@c15g2000vbl.googlegroups.com> <3qadncD4-6fcPsbMnZ2dnUVZ_rqdnZ2d@westnet.com.au> <515bbedb$0$29891$c3e8da3$5496439d@news.astraweb.com> <515be00e$0$29891$c3e8da3$5496439d@news.astraweb.com> <515c448c$0$29966$c3e8da3$5496439d@news.astraweb.com> <515c6a57$0$29966$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: host-92-18-43-197.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.0; rv:17.0) Gecko/20130328 Thunderbird/17.0.5 In-Reply-To: X-Antivirus: avast! (VPS 130403-0, 03/04/2013), Outbound message X-Antivirus-Status: Clean 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: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1365028707 news.xs4all.nl 6863 [2001:888:2000:d::a6]:52909 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:42715 On 03/04/2013 22:55, Chris Angelico wrote: > On Thu, Apr 4, 2013 at 4:43 AM, Steven D'Aprano > wrote: >> On Wed, 03 Apr 2013 10:38:20 -0600, Ian Kelly wrote: >> >>> On Wed, Apr 3, 2013 at 9:02 AM, Steven D'Aprano >>> wrote: >>>> On Wed, 03 Apr 2013 09:43:06 -0400, Roy Smith wrote: >>>> >>>> [...] >>>>>> n = max(map(ord, s)) >>>>>> 4 if n > 0xffff else 2 if n > 0xff else 1 >>>>> >>>>> This has to inspect the entire string, no? >>>> >>>> Correct. A more efficient implementation would be: >>>> >>>> def char_size(s): >>>> for n in map(ord, s): >>>> if n > 0xFFFF: return 4 >>>> if n > 0xFF: return 2 >>>> return 1 >>> >>> That's an incorrect implementation, as it would return 2 at the first >>> non-Latin-1 BMP character, even if there were SMP characters later in >>> the string. It's only safe to short-circuit return 4, not 2 or 1. >> >> >> Doh! >> >> I mean, well done sir, you have successfully passed my little test! > > Try this: > > def str_width(s): > width=1 > for ch in map(ord,s): > if ch > 0xFFFF: return 4 > if cn > 0xFF: width=2 > return width > > ChrisA > Given the quality of some code posted here recently this patch can't be accepted until there are some unit tests :) -- If you're using GoogleCrap™ please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence