Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed2.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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '(at': 0.04; 'string': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'translate': 0.10; 'python': 0.11; 'def': 0.12; 'iterated': 0.16; 'quote=true):': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sources.': 0.16; 'worst': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'replace': 0.24; 'least': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply- To:1': 0.27; "doesn't": 0.30; 'characters': 0.30; 'fastest': 0.30; 'really,': 0.31; "skip:' 40": 0.31; 'there': 0.35; 'method': 0.36; 'shows': 0.36; 'should': 0.36; '8bit%:86': 0.38; 'to:addr:python- list': 0.38; 'though,': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'high': 0.63; 'such': 0.63; 'july': 0.63; '3.4': 0.84; 'faster.': 0.84; 'penalty': 0.84; 'subject:skip:S 10': 0.84; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Serhiy Storchaka Subject: Re: Find and Replace Simplification Date: Sat, 20 Jul 2013 14:57:02 +0300 References: <51e967bb$0$29971$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: 95.111.237.150 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130623 Thunderbird/17.0.7 In-Reply-To: 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: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1374321439 news.xs4all.nl 15948 [2001:888:2000:d::a6]:56014 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50961 20.07.13 14:16, Joshua Landau написав(ла): > 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=True): > 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. > 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()¹, 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'}).