Path: csiph.com!usenet.pasdenom.info!gegeweb.org!newsfeed0.kamp.net!newsfeed.kamp.net!feeder1.cambriumusenet.nl!82.197.223.103.MISMATCH!feeder3.cambriumusenet.nl!feed.tweaknews.nl!194.134.4.91.MISMATCH!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.053 X-Spam-Evidence: '*H*': 0.90; '*S*': 0.00; '(so': 0.07; 'method.': 0.07; 'stops': 0.07; 'ascii': 0.09; 'builtin': 0.09; 'iterate': 0.09; "they've": 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '"list': 0.16; '3.3,': 0.16; 'dict': 0.16; 'force.': 0.16; 'hypothetical': 0.16; 'mappings,': 0.16; 'objects.': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'obviously': 0.18; 'solution.': 0.20; 'code,': 0.22; 'portion': 0.22; 'cc:addr:python.org': 0.22; "aren't": 0.24; 'entries': 0.24; 'versions': 0.24; 'cc:2**0': 0.24; 'least': 0.26; 'header:In- Reply-To:1': 0.27; "doesn't": 0.30; 'bigger': 0.30; 'characters': 0.30; 'especially': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'too.': 0.31; 'types.': 0.31; 'lists': 0.32; 'are:': 0.33; 'cases': 0.33; 'checking': 0.33; 'there,': 0.34; "i'd": 0.34; 'could': 0.34; 'problem': 0.35; 'skip:s 30': 0.35; 'something': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'really': 0.36; 'done': 0.36; 'method': 0.36; 'useful': 0.36; 'should': 0.36; 'list': 0.37; 'list.': 0.37; 'mapping': 0.38; 'pm,': 0.38; 'even': 0.60; 'easy': 0.60; 'dave': 0.60; 'july': 0.63; 'more': 0.64; 'talking': 0.65; 'here': 0.66; 'covers': 0.68; 'gotten': 0.74; 'obvious': 0.74; '100': 0.79; '.replace': 0.84; 'dict.': 0.84; 'faster.': 0.84; 'imagine,': 0.84; 'optimisation': 0.84; 'subject:skip:S 10': 0.84; 'we?': 0.84; 'afford': 0.91; 'angel': 0.91; 'examine': 0.93; 'imagine': 0.93; '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=D1fvre15Zw2Y/ht6XL1Ja7z217EF02OaJ9MGDrGPXTQ=; b=oqUDPzXFuhh9+RHDQEdf1MPn4Orb5/qegMgskymVqeX15t7KbCbX6UxNrGHarACfmn au7zwvM3LhrxRaRg6oRaFnzO2oNxVgqHz4a7oaqbhRFJs0ytJfQADpaibPXOvMvZlIYZ V7ZfgaYAgBrAlP7m+RYm1xbdFuQ6Rz3zvqtGXhwuJgPaBI7iUcYpqdcL/SWzsWD1Ge4f tsGGwEjkhRQSHq/JTlmOAj9Olb5pRbn8cDrlj2t/HXsmYrB36sOAGlCRsnS6oSUH24iO BFQ4v40JuvzP8gQod3/gwRVc2idDf3oyAxpYbLJL26WuDsgHYErWL5iEG8PuPgWVhF53 0HjA== X-Received: by 10.152.88.78 with SMTP id be14mr9947884lab.19.1374359644089; Sat, 20 Jul 2013 15:34:04 -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 23:33:23 +0100 X-Google-Sender-Auth: uIaKLPxaiY6qxtL4gJ_c9XEch5I Subject: Re: Find and Replace Simplification To: Dave Angel 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: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1374359653 news.xs4all.nl 15956 [2001:888:2000:d::a6]:52551 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50981 On 20 July 2013 22:56, Dave Angel wrote: > On 07/20/2013 02:37 PM, Joshua Landau wrote: >> >> The problem can be solved, I'd imagine, for builtin types. Just build >> an internal representation upon calling .translate that's faster. It's >> especially easy in the list case > > What "list case"? list doesn't have a replace() method or translate() > method. I mean some_str.translate(some_list). >> -- just build a C array=C2=B9 at the start >> mapping int -> int and then have really fast C mapping speeds. > > > As long as you can afford to have a list with a billion or so entries in = it. > We are talking about strings and version 3.3, aren't we? Of course, one > could always examine the mapping object (table) and see what the max valu= e > was, and only build a "C array" if it was smaller than say 50,000. When talking about some_str.translate(some_list), this doesn't apply very much -- they've already gotten a much bigger Python list. In the dict case=C2=B2 I don't actually want to jump to the conclusion that one should do array-based mappings because I can see the obvious downsides and it's obviously not good to have 100 cases in there, *but* I still think that there's a solution. Here are some ideas: =C2=B7 Latin and ASCII can obviously be done with a C array, and I imagine that covers at least a fair portion of use-cases. =C2=B7 If you only have a few characters in the mapping (so sys.getsizeof is small) then it'll be a lot faster to just iterate through a C list instead of checking the dict. =C2=B7 Other cases are: =C2=B7 Full-character-set or equiv. mappings, which are already faster than .replace(). Those should really be re-made into lists so that the list optimisation can take place, and lists are much faster even in versions without these hypothetical optimizations, too. =C2=B7 Custom objects. There's nothing we can do here. I realise that this is a lot more code, so it's not something I'm going to try to force. However, I think it's useful if it stops people using .replace in a loop ;). =C2=B2 some_str.translate(some_dict)