[Tutor] loop performance in global namespace (python-2.6.1)
kent37 at tds.net
Thu Mar 12 16:13:33 CET 2009
On Thu, Mar 12, 2009 at 8:13 AM, Poor Yorick
<org.python.pythonlist at pooryorick.com> wrote:
> In the following snippet, the loop in the global namespace takes twice as
> as the loop in the function namespace. Why?
Because local name lookup is faster than global name lookup. Local
variables are stored in an array in the stack frame and accessed by
index. Global names are stored in a dict and accessed with dict access
One trick for optimizing a function is to make local variable copies
of any globals that are referenced more than once per function call,
so they can use the faster lookup.
More information about the Tutor