Calling a function is faster than not calling it?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun May 10 10:49:50 EDT 2015


On Sun, 10 May 2015 08:43 pm, Peter Otten wrote:

> A significant part of the extra time is apparently spent on stack
> inspection:

I don't know what you mean by "stack inspection", or how you come to that
conclusion.


> $ python3 -m timeit -s 'f = (lambda: 42); code = f.__code__; ns = {}'
> 'f()' 10000000 loops, best of 3: 0.179 usec per loop
> 
> $ python3 -m timeit -s 'f = (lambda: 42); code = f.__code__; ns = {}'
> 'eval(code)'
> 1000000 loops, best of 3: 0.852 usec per loop
> 
> $ python3 -m timeit -s 'f = (lambda: 42); code = f.__code__; ns = {}'
> 'eval(code, ns)'
> 1000000 loops, best of 3: 0.433 usec per loop

Curious.

If I'm reading that correctly, supplying an explicit namespace cuts the time
by a factor of two. That surprises me, as your function doesn't do any name
lookups in the body of the function, so I would have thought that would be
irrelevant.


> $ python3 -m timeit -s 'f = (lambda: 42); code = f.__code__; ns = {}'
> 'eval; ns; f()'
> 1000000 loops, best of 3: 0.263 usec per loop

So, roughly speaking, calling eval(code, ns) takes about 1.6 times as long
as calling the function; calling eval(code) without providing a namespace
takes about 3.2 times as long. That's roughly consistent with the results
I'm getting.  



-- 
Steven




More information about the Python-list mailing list