Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.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.066 X-Spam-Evidence: '*H*': 0.87; '*S*': 0.00; 'string': 0.09; 'builtin': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rebuild': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'wrote:': 0.18; 'not,': 0.20; 'seems': 0.21; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'faster,': 0.31; 'but': 0.35; 'i.e.': 0.36; 'should': 0.36; 'too': 0.37; 'implement': 0.38; '8bit%:86': 0.38; 'mapping': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'analyze': 0.60; 'most': 0.60; 'real': 0.63; 'such': 0.63; 'july': 0.63; 'received:46': 0.66; 'overall': 0.69; 'theoretical': 0.74; '.replace': 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: Sun, 21 Jul 2013 15:28:32 +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: 46.211.183.104 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: 20 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1374409726 news.xs4all.nl 16003 [2001:888:2000:d::a6]:36714 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:51009 21.07.13 14:29, Joshua Landau написав(ла): > On 21 July 2013 08:44, Serhiy Storchaka wrote: >> 20.07.13 20:03, Joshua Landau написав(ла): >> >>> 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. >> >> You should analyze overall mapping and reorder items in right order (if it >> possible), i.e. '&' should be replaced before '<' in html.escape. This extra >> work is too large for most real input. > > I don't understand. What items are you reordering? mapping.items(). We can implement s.translate({ord('<'): '<', ord('&'): '&'}) as s.replace('&', '&').replace('<', '<'), but not as s.replace('<', '<').replace('&', '&').