Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:not': 0.03; 'explicit': 0.07; 'python3': 0.07; 'function,': 0.09; 'namespace': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; '10000000': 0.16; 'curious.': 0.16; 'function;': 0.16; 'globals': 0.16; 'lookups': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'reminded': 0.16; 'supplying': 0.16; 'surprises': 0.16; 'two.': 0.16; 'prevent': 0.16; 'wrote:': 0.18; 'do.': 0.18; 'stack': 0.19; '(the': 0.22; 'header:User-Agent:1': 0.23; 'mention': 0.26; 'header:X -Complaints-To:1': 0.27; 'function': 0.29; 'skip:p 30': 0.29; "doesn't": 0.30; "i'm": 0.30; 'code': 0.31; '3.2': 0.31; 'apparently': 0.31; "d'aprano": 0.31; 'factor': 0.31; 'long.': 0.31; 'null)': 0.31; 'null;': 0.31; 'piece': 0.31; 'steven': 0.31; 'run': 0.32; 'actual': 0.34; 'could': 0.34; 'there': 0.35; 'consistent': 0.36; 'subject:?': 0.36; 'so,': 0.37; 'turn': 0.37; 'being': 0.38; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'how': 0.40; 'providing': 0.61; 'first': 0.61; 'times': 0.62; 'name': 0.63; 'results': 0.69; '1.6': 0.84; '2015': 0.84; 'cuts': 0.84; 'otten': 0.84; 'was:': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Calling a function is faster than not calling it? Date: Sun, 10 May 2015 18:14:17 +0200 Organization: None References: <554f2bb6$0$13011$c3e8da3$5496439d@news.astraweb.com> <554f700f$0$13011$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd9920.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1431274466 news.xs4all.nl 2900 [2001:888:2000:d::a6]:45484 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:90293 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.