Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.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.017 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'string.': 0.05; 'subject:Python': 0.06; 'correct.': 0.07; 'character,': 0.09; 'width': 0.09; 'def': 0.12; 'be:': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'incorrect': 0.16; 'mean,': 0.16; 'roy': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'thu,': 0.19; 'later': 0.20; '>>>': 0.22; 'string,': 0.24; 'this:': 0.26; 'subject:/': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'characters': 0.30; 'message-id:@mail.gmail.com': 0.30; '>>>>': 0.31; "d'aprano": 0.31; 'inspect': 0.31; 'steven': 0.31; 'received:209.85': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'done': 0.36; 'received:209': 0.37; 'to:addr:python-list': 0.38; 'little': 0.38; 'to:addr:python.org': 0.39; 'even': 0.60; 'ian': 0.60; 'entire': 0.61; 'first': 0.61; 'more': 0.64; 'smith': 0.68; 'safe': 0.72; 'sir,': 0.81; '9:02': 0.84; 'subject:long': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=+5lbZrTv3Zj5yIusW4GwrCnIerJofSeXzI3Bd1kNmTw=; b=anSEb4j7w2MLxpS2pAlgFjXdbBQE3XWfaqI6N91MZKz8pWmC/vlw0Xj7T9eS++Pupc xgnIrSDDoG5ryq1PhreK424PMQy8lK7kD1/hHNRTfxm31eKKAF9qcPZCtsRYFKuyNjBA u4a/n9CGMiwP5QBkyrR1Z8tXJP/QrKX8OY7FDpMazL7pbGreXhKkbJUDOs+eRcewZIfg 9QgT1XUD7E52eHokGYKcflal5Ai0q75y6oSiIw+Ni8gD7DWRXaYmsjsaIgY52hxPtH60 0fwtjJLVwFqm2ig7USVo2Mluat4WhbLj5v8kYpdXZjIKK119F4Fabayy4c/IWsM4zZWz T50A== MIME-Version: 1.0 X-Received: by 10.220.231.199 with SMTP id jr7mr2691504vcb.70.1365026143285; Wed, 03 Apr 2013 14:55:43 -0700 (PDT) In-Reply-To: <515c6a57$0$29966$c3e8da3$5496439d@news.astraweb.com> 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> Date: Thu, 4 Apr 2013 08:55:43 +1100 Subject: Re: Performance of int/long in Python 3 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: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1365026152 news.xs4all.nl 6936 [2001:888:2000:d::a6]:50235 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:42707 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