Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #90293
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Subject | Re: Calling a function is faster than not calling it? |
| Date | 2015-05-10 18:14 +0200 |
| Organization | None |
| References | <554f2bb6$0$13011$c3e8da3$5496439d@news.astraweb.com> <mailman.304.1431254634.12865.python-list@python.org> <554f700f$0$13011$c3e8da3$5496439d@news.astraweb.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.314.1431274466.12865.python-list@python.org> (permalink) |
Steven D'Aprano wrote:
> 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.
I had a quick look at the implementation in bltinmodule.c, and the only
piece of code that I could prevent from being run was:
if (globals == Py_None) {
globals = PyEval_GetGlobals();
if (locals == Py_None) {
locals = PyEval_GetLocals();
if (locals == NULL)
return NULL;
}
}
When there was an actual speed-up I also had a look at
PyEval_GetGlobals/Locals() which in turn call
PyEval_GetFrame()
and
PyEvalPyFrame_FastToLocalsWithError()
whatever these do. (The first function reminded me of sys._getframe() hence
the mention of stack inspection)
>> $ 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.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Calling a function is faster than not calling it? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-10 19:58 +1000
Re: Calling a function is faster than not calling it? Christian Gollwitzer <auriocus@gmx.de> - 2015-05-10 12:34 +0200
Re: Calling a function is faster than not calling it? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-11 01:04 +1000
Re: Calling a function is faster than not calling it? Peter Otten <__peter__@web.de> - 2015-05-10 12:43 +0200
Re: Calling a function is faster than not calling it? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-11 00:49 +1000
Re: Calling a function is faster than not calling it? Peter Otten <__peter__@web.de> - 2015-05-10 18:14 +0200
Re: Calling a function is faster than not calling it? Ian Kelly <ian.g.kelly@gmail.com> - 2015-05-10 10:25 -0600
Re: Calling a function is faster than not calling it? Terry Reedy <tjreedy@udel.edu> - 2015-05-10 12:37 -0400
Re: Calling a function is faster than not calling it? BartC <bc@freeuk.com> - 2015-05-10 22:08 +0100
Re: Calling a function is faster than not calling it? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-11 13:58 +1000
Re: Calling a function is faster than not calling it? BartC <bc@freeuk.com> - 2015-05-11 10:50 +0100
Re: Calling a function is faster than not calling it? Skip Montanaro <skip.montanaro@gmail.com> - 2015-05-11 09:12 -0500
Re: Calling a function is faster than not calling it? BartC <bc@freeuk.com> - 2015-05-11 16:01 +0100
Re: Calling a function is faster than not calling it? Skip Montanaro <skip.montanaro@gmail.com> - 2015-05-11 10:13 -0500
Re: Calling a function is faster than not calling it? Tony the Tiger <tony@tiger.invalid> - 2015-05-15 01:35 +0000
Re: Calling a function is faster than not calling it? Stefan Behnel <stefan_ml@behnel.de> - 2015-05-11 08:11 +0200
Re: Calling a function is faster than not calling it? Piet van Oostrum <piet@vanoostrum.org> - 2015-06-22 23:49 +0200
csiph-web