Re: Calling a function is faster than not calling it?

2015-06-22 Thread Piet van Oostrum
Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: Why is calling a function faster than bypassing the function object and evaluating the code object itself? And not by a little, but by a lot? Here I have a file, eval_test.py: # === cut === from timeit import Timer def func

Re: Calling a function is faster than not calling it?

2015-05-11 Thread Stefan Behnel
Steven D'Aprano schrieb am 10.05.2015 um 11:58: Why is calling a function faster than bypassing the function object and evaluating the code object itself? And not by a little, but by a lot? Here I have a file, eval_test.py: # === cut === from timeit import Timer def func(): a = 2

Re: Calling a function is faster than not calling it?

2015-05-11 Thread BartC
On 11/05/2015 04:58, Steven D'Aprano wrote: On Mon, 11 May 2015 07:08 am, BartC wrote: On 10/05/2015 10:58, Steven D'Aprano wrote: def func(): a = 2 b = 3 c = 4 return (a+b)*(a-b)/(a*c + b*c) print (min(t1.repeat(repeat=10))) print (min(t2.repeat(repeat=10)))

Re: Calling a function is faster than not calling it?

2015-05-11 Thread Skip Montanaro
On Mon, May 11, 2015 at 10:01 AM, BartC b...@freeuk.com wrote: (1) It has an extra argument ('code'), in addition to any normal arguments of func (0 in this case) Which might well push execution down the unoptimized code path. Also, ISTR that Steven's original timeit runs tacked on a standalone

Re: Calling a function is faster than not calling it?

2015-05-11 Thread Skip Montanaro
On Mon, May 11, 2015 at 4:50 AM, BartC b...@freeuk.com wrote: You just seem surprised that using eval() to do this is slower than a direct call. Well, it is surprising. Most uses of eval() are to evaluate Python expressions in string form. That I expect to be quite slow, given the

Re: Calling a function is faster than not calling it?

2015-05-11 Thread BartC
On 11/05/2015 15:12, Skip Montanaro wrote: On Mon, May 11, 2015 at 4:50 AM, BartC b...@freeuk.com mailto:b...@freeuk.com wrote: You just seem surprised that using eval() to do this is slower than a direct call. Well, it is surprising. Most uses of eval() are to evaluate Python

Re: Calling a function is faster than not calling it?

2015-05-10 Thread BartC
On 10/05/2015 10:58, Steven D'Aprano wrote: from timeit import Timer def func(): a = 2 b = 3 c = 4 return (a+b)*(a-b)/(a*c + b*c) code = func.__code__ assert func() == eval(code) t1 = Timer(eval; func(), setup=from __main__ import func) t2 = Timer(eval(code), setup=from

Re: Calling a function is faster than not calling it?

2015-05-10 Thread Steven D'Aprano
On Mon, 11 May 2015 07:08 am, BartC wrote: On 10/05/2015 10:58, Steven D'Aprano wrote: from timeit import Timer def func(): a = 2 b = 3 c = 4 return (a+b)*(a-b)/(a*c + b*c) code = func.__code__ assert func() == eval(code) t1 = Timer(eval; func(), setup=from

Re: Calling a function is faster than not calling it?

2015-05-10 Thread Peter Otten
Steven D'Aprano wrote: Why is calling a function faster than bypassing the function object and evaluating the code object itself? And not by a little, but by a lot? Directly eval'ing the code object is easily more than twice as expensive than calling the function, but calling the function

Calling a function is faster than not calling it?

2015-05-10 Thread Steven D'Aprano
Why is calling a function faster than bypassing the function object and evaluating the code object itself? And not by a little, but by a lot? Here I have a file, eval_test.py: # === cut === from timeit import Timer def func(): a = 2 b = 3 c = 4 return (a+b)*(a-b)/(a*c + b*c

Re: Calling a function is faster than not calling it?

2015-05-10 Thread Christian Gollwitzer
Am 10.05.15 um 11:58 schrieb Steven D'Aprano: Why is calling a function faster than bypassing the function object and evaluating the code object itself? And not by a little, but by a lot? Here I have a file, eval_test.py: # === cut === from timeit import Timer def func(): a = 2 b

Re: Calling a function is faster than not calling it?

2015-05-10 Thread Terry Reedy
On 5/10/2015 5:58 AM, Steven D'Aprano wrote: Why is calling a function faster than bypassing the function object and evaluating the code object itself? And not by a little, but by a lot? Here I have a file, eval_test.py: # === cut === from timeit import Timer def func(): a = 2 b = 3

Re: Calling a function is faster than not calling it?

2015-05-10 Thread Steven D'Aprano
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()'

Re: Calling a function is faster than not calling it?

2015-05-10 Thread Peter Otten
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 =

Re: Calling a function is faster than not calling it?

2015-05-10 Thread Steven D'Aprano
On Sun, 10 May 2015 08:34 pm, Christian Gollwitzer wrote: Am 10.05.15 um 11:58 schrieb Steven D'Aprano: Why is calling a function faster than bypassing the function object and evaluating the code object itself? And not by a little, but by a lot? Here I have a file, eval_test.py: # === cut

Re: Calling a function is faster than not calling it?

2015-05-10 Thread Ian Kelly
On Sun, May 10, 2015 at 10:14 AM, Peter Otten __pete...@web.de wrote: 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