Curious to see alternate approach on a search/replace via regex

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Feb 8 00:59:38 CET 2013


rh wrote:

> On Fri, 08 Feb 2013 09:45:41 +1100
> Steven D'Aprano <steve+comp.lang.python at pearwood.info> wrote:
> 
>> rh wrote:
>> 
>> > I am using 2.7.3 and I put the re.compile outside the function and
>> > it performed faster than urlparse. I don't print out the data.
>> 
>> I find that hard to believe. re.compile caches its results, so except
>> for the very first time it is called, it is very fast -- basically a
>> function call and a dict lookup. I find it implausible that a
>> micro-optimization such as you describe could be responsible for
>> speeding the code up by over 33%.
> 
> Not sure where you came up with that number. Maybe another post?

That number comes from my post, which you replied to.

http://mail.python.org/pipermail/python-list/2013-February/640056.html

By the way, are you aware that you are setting the X-No-Archive header on
your posts?



> I never gave any numbers, just comparisons.
> 
>> 
>> But since you don't demonstrate any actual working code, you could be
>> correct, or you could be timing it wrong. Without seeing your timing
>> code, my guess is that you are doing it wrong. Timing code is tricky,
>> which is why I always show my work. If I get it wrong, someone will
>> hopefully tell me. Otherwise, I might as well be making up the
>> numbers.
> 
> re.compile
> starttime = time.time()
> for i in range(numloops):
>     u2f()
> 
> msg = '\nElapsed {0:.3f}'.format(time.time() - starttime)
> print(msg)


I suggest you go back to my earlier post, the one you responded to, and look
at how I use the timeit module to time small code snippets. Then read the
documentation for it, and the comments in the source code. If you can get
hold of the Python Cookbook, read Tim Peters' comments in that.

http://docs.python.org/2/library/timeit.html
http://docs.python.org/3/library/timeit.html



Oh, one last thing... pulling out "re.compile" outside of the function does
absolutely nothing. You don't even compile anything. It basically looks up
that a compile function exists in the re module, and that's all.



-- 
Steven




More information about the Python-list mailing list