Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!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.023 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; '(at': 0.04; 'string': 0.09; 'builtin': 0.09; 'translate': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'iterated': 0.16; 'quote=true):': 0.16; 'rebuild': 0.16; 'sources.': 0.16; 'worst': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'not,': 0.20; 'seems': 0.21; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'replace': 0.24; 'cc:2**0': 0.24; 'least': 0.26; 'header:In-Reply-To:1': 0.27; "doesn't": 0.30; 'characters': 0.30; 'fastest': 0.30; 'message-id:@mail.gmail.com': 0.30; 'faster,': 0.31; 'really,': 0.31; "skip:' 40": 0.31; 'received:google.com': 0.35; 'there': 0.35; 'method': 0.36; 'shows': 0.36; "i'll": 0.36; 'should': 0.36; '8bit%:86': 0.38; 'does': 0.39; 'though,': 0.39; 'how': 0.40; 'high': 0.63; 'such': 0.63; 'july': 0.63; 'to:addr:gmail.com': 0.65; 'theoretical': 0.74; '.replace': 0.84; '3.4': 0.84; 'faster.': 0.84; 'penalty': 0.84; 'subject:skip:S 10': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type :content-transfer-encoding; bh=FcUyPPUO0uMznnAWAO5tIxwNblikryPeEAzEeF2ISPs=; b=LTZzSgQgktTy9oiXD1aglZj2hB0ekfHIxIURtULMLjxcedWvt8oFUhrThHo9SjHKZ+ CaTWpnZwnVwcQ+Vgf5XYLv0MOUZu8vJVK5E4tPlEHHnZQhNmBuJaUvenF2nUCQnEiuem A4pyh40MmTWj4YZsLE7Wx/jLAKvkAG3gRRTHLTIJuG7d6Na7S/FPGnPtREhCC5rK3FS/ C+s4/xTiyPiCkkKWWLFJqd2RNDXLtCGtYXJWljRtI5TbRZSWn3/j4m9MpWa4Mnzt+VlA J1OjAW8Uj7xSzD2C2IdCe+5v8FUq7bXjuyF8ThSp8Ajng0N+OwYSsyv0hizspKEQi/Lv stjw== X-Received: by 10.112.61.199 with SMTP id s7mr9425555lbr.53.1374339878618; Sat, 20 Jul 2013 10:04:38 -0700 (PDT) MIME-Version: 1.0 Sender: joshua.landau.ws@gmail.com In-Reply-To: References: <51e967bb$0$29971$c3e8da3$5496439d@news.astraweb.com> From: Joshua Landau Date: Sat, 20 Jul 2013 18:03:58 +0100 X-Google-Sender-Auth: 1ZOTQ_PVS7vXH4jVNpBMZNVBYCA Subject: Re: Find and Replace Simplification To: Serhiy Storchaka Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list 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: 1374339885 news.xs4all.nl 15883 [2001:888:2000:d::a6]:53883 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50970 On 20 July 2013 12:57, Serhiy Storchaka wrote: > 20.07.13 14:16, Joshua Landau =D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=B2(= =D0=BB=D0=B0): >> >> On 19 July 2013 18:29, Serhiy Storchaka wrote: >>> >>> The string replace() method is fastest (at least in Python 3.3+). See >>> implementation of html.escape() etc. >> >> >> def escape(s, quote=3DTrue): >> if quote: >> return s.translate(_escape_map_full) >> return s.translate(_escape_map) >> >> I fail to see how this supports the assertion that str.replace() is >> faster. > > > And now look at Python 3.4 sources. I'll just trust you ;). >> However, some quick timing shows that translate has a very >> high penalty for missing characters and is a tad slower any way. >> >> Really, though, there should be no reason for .translate() to be >> slower than replace -- at worst it should just be "reduce(lambda s, >> ab: s.replace(*ab), mapping.items()=C2=B9, original_str)" and end up the >> *same* speed as iterated replace. > > > It doesn't work such way. Consider > 'ab'.translate({ord('a'):'b',ord('b'):'a'}). *sad* Still, it seems to me that it should be optimizable for sensible builtin types such that .translate is significantly faster, as there's no theoretical extra work that .translate *has* to do that .replace does not, and .replace also has to rebuild the string a lot of times.