Overhead of (was Reasoning behind) nested scope

Nigel Rowe rho at see.signature.invalid
Thu Aug 5 03:17:17 EDT 2004


Peter Otten wrote:

> Nigel Rowe wrote:
> 
>> Peter Otten wrote:
>> 
>>> Andy Baker wrote:
>>> 
>

<snip>

>> What work is actually done when the 
>>         'nested function creation code of the "inner" function'
>> is executed?

< snip >

> 
> In the example you get three extra instructions, LOAD_CONST, MAKE_FUNCTION
> and STORE_FAST (and earn the slight benefit that inner is now a local
> instead of a global). A constant code object is used, meaning compilation
> takes place at most once when the module is loaded. There is a dedicated
> op-code, so function creation should be faster than "normal" creation of a
> class instance.
> 
> Now this is all nice and dandy, but how do the two contenders perform?
> 
> $ timeit.py -s"def inner(): pass" -s"def outer(): return inner" "outer()"
> 1000000 loops, best of 3: 0.469 usec per loop
> $ timeit.py -s"def outer():" -s"  def inner(): pass" -s"  return inner"
> "outer()"
> 1000000 loops, best of 3: 1.12 usec per loop
> 
> i. e. nesting the two functions roughly doubles execution time.
> However, creation of an inner function often will only take a small
> fraction of the total time spent in the outer function - in the end it's
> just a matter of style.
> 
> I use inner functions only when they depend on the local context because I
> think it nicely discriminates closures from helpers. I tend to omit
> implicit parameters even for helper funtions, therefore nesting them would
> gain me nothing.
> 
> Peter

Thanks Peter, I didn't know about timeit.py (it's now on my $PATH).

I don't think I'm going to worry about the overhead (except in deeply nested
loops).  From some quick testing it looks like the overhead is about the
same as 1.5 times
        a=b[10:20] # where b="the quick brown jox jumps over the lazy dog"
and less than 1/2 of
        a=math.sin(0)

So if the nested code is easier to read, in it goes.
-- 
        Nigel Rowe
        A pox upon the spammers that make me write my address like..
                rho (snail) swiftdsl (stop) com (stop) au



More information about the Python-list mailing list