Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!news2.euro.net!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.033 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'string.': 0.05; 'subject:Python': 0.06; 'correct.': 0.07; 'character,': 0.09; 'def': 0.12; 'be:': 0.16; 'incorrect': 0.16; 'roy': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'later': 0.20; '>>>': 0.22; 'string,': 0.24; 'subject:/': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'characters': 0.30; 'message-id:@mail.gmail.com': 0.30; "d'aprano": 0.31; 'inspect': 0.31; 'steven': 0.31; 'received:209.85': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'received:209': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'even': 0.60; 'entire': 0.61; 'first': 0.61; 'more': 0.64; 'smith': 0.68; 'safe': 0.72; '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=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:content-type; bh=XWRdcLXz7YvPTSt5KeEYlv73hgI1MK4ac2bQEooGnnA=; b=ZPtghoGDSuyDATc7L1fdTMwy2pkfDq3vnmLTG4yja3udm3m4NHTkf1MS8ovuvdxWDf 9yQSUszt4S3ESdIOIUJhDAgj/F7BzbHPcq6xc5g3Ys2rygZLFKtlx4OYdmhoRgm3FTa4 YubZjX/TXiZY2t6CaNQ6i5+9TRVgxrbEXg+IBhVozToQa3OarSZ/254AMzwPqiiPCVJ0 KuAouGBphRt+KveSdM47JifZvvqCHoNCknY8CgyUEUVQRCRzP3et4USBuAviLvVDzXyt diF6l8kw2BzIFZJS/CDcemNQWRy4ygozdDV+OExzYcI2X77nuQ6Ri8LgIDwsSXgxPIzO svRQ== X-Received: by 10.66.84.74 with SMTP id w10mr2369635pay.214.1365007140304; Wed, 03 Apr 2013 09:39:00 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <515c448c$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> From: Ian Kelly Date: Wed, 3 Apr 2013 10:38:20 -0600 Subject: Re: Performance of int/long in Python 3 To: Python 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: 21 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1365007144 news.xs4all.nl 6977 [2001:888:2000:d::a6]:48782 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:42685 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.