Performance on local constants?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Dec 27 04:47:20 EST 2007


En Sun, 23 Dec 2007 03:55:07 -0300, John Machin <sjmachin at lexicon.net>  
escribió:
> On Dec 23, 2:39 pm, "Terry Reedy" <tjre... at udel.edu> wrote:

>> I will presume you are merely joking, but for the benefit of any  
>> beginning
>> programmers reading this, the closure above is a standard functional  
>> idiom
>> for partial evaluation of a function (in this this, re.search(crex,txt))
>
> Semi-joking; I thought that your offering of this:
>
> def searcher(rex):
>     crex = re.compile(rex)
>     def _(txt):
>         return crex.search(txt)
>     return _
> foo_searcher = searcher('foo')
>
> was somewhat over-complicated, and possibly slower than already-
> mentioned alternatives. The standard idiom etc etc it may be, but the
> OP was interested in getting overhead out of his re searching loop.
> Let's trim it a bit.
>
> step 1:
> def searcher(rex):
>     crexs = re.compile(rex).search
>     def _(txt):
>         return crexs(txt)
>     return _
> foo_searcher = searcher('foo')
>
> step 2:
> def searcher(rex):
>     return re.compile(rex).search
> foo_searcher = searcher('foo')
>
> step 3:
> foo_searcher = re.compile('foo').search

Nice derivation! Like the word-stairs game: love -> rove -> rave -> have  
-> hate

-- 
Gabriel Genellina




More information about the Python-list mailing list