Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!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.050 X-Spam-Evidence: '*H*': 0.90; '*S*': 0.00; '"""': 0.05; 'cache': 0.05; '*is*': 0.09; 'cache:': 0.16; 'caching': 0.16; 'subject:search': 0.16; 'wrote:': 0.17; 'thu,': 0.17; '>>>': 0.18; 'feb': 0.19; 'import': 0.21; 'fraction': 0.22; 'setup.': 0.22; 'header:In- Reply-To:1': 0.25; 'compiled': 0.27; 'message-id:@mail.gmail.com': 0.27; 'regular': 0.27; 'subject:/': 0.28; "we're": 0.30; 'to:addr :python-list': 0.33; 'received:google.com': 0.34; 'done': 0.34; 'clear': 0.35; 'needed': 0.35; 'whatever': 0.35; 'pm,': 0.35; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'add': 0.36; 'execute': 0.37; 'being': 0.37; 'skip:3 10': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'think': 0.40; 'times': 0.63; 'here': 0.65; 'url:a': 0.72; 'savings': 0.75; '2013': 0.84; 'subject:via': 0.84; 'to:name:python': 0.84; 'hand,': 0.97 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=nR0XGlactrkodl5O5qKbWrWaJv5KIgmqcYW2yklAynw=; b=UyR8czhzM0z6dfXocVpZmkDddya06D6gt5TJFpnrpIbOiVZVy5jja7QDonAmHdm8oU iwY6HyLJH/+IyN523aIa6AUubndOi7j12t7YC+vlEO7AzVfDNwxHF5rPtDj8bJdBrKsb zcRssZHLZvUQowoiOLg+YdOu8Dt7WbKiD/NxDrXaH3teJliwx6aj/Kr12AXMFMBy9EHC 314yZmWVDfbJmc++r784Cagbr8FRP4G+E5sEMVaRCRtf/PifnvrduiefDCzkWbrmp39J nWIbWjJnrqCu1QX++CU/Mf80N9IdDWXGxplUoSSPryXguVnAS3uNmm6ryAv7H1CO0Oo+ B/DA== X-Received: by 10.66.52.50 with SMTP id q18mr11589319pao.16.1360285720325; Thu, 07 Feb 2013 17:08:40 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: <511319c7$0$21812$c3e8da3$76491128@news.astraweb.com> <51142e96$0$6512$c3e8da3$5496439d@news.astraweb.com> <51143feb$0$29974$c3e8da3$5496439d@news.astraweb.com> From: Ian Kelly Date: Thu, 7 Feb 2013 18:08:00 -0700 Subject: Re: Curious to see alternate approach on a search/replace via regex 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: 23 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360285724 news.xs4all.nl 6919 [2001:888:2000:d::a6]:40980 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38390 On Thu, Feb 7, 2013 at 5:55 PM, Ian Kelly wrote: > Whatever caching is being done by re.compile, that's still a 24% > savings by moving the compile calls into the setup. On the other hand, if you add an re.purge() call to the start of t1 to clear the cache: >>> t3 = Timer(""" ... re.purge() ... nx = re.compile(r'https?://(.+)$') ... v = nx.search(u).group(1) ... ux = re.compile(r'([-:./?&=]+)') ... ux.sub('_', v)""", """ ... import re ... u = 'http://alongnameofasite1234567.com/q?sports=run&a=1&b=1'""") >>> min(t3.repeat(number=10000)) 3.5532990924824617 Which is approximately 30 times slower, so clearly the regular expression *is* being cached. I think what we're seeing here is that the time needed to look up the compiled regular expression in the cache is a significant fraction of the time needed to actually execute it.