On Thu, Mar 12, 2009 at 8:13 AM, Poor Yorick <org.python.pythonl...@pooryorick.com> wrote: > In the following snippet, the loop in the global namespace takes twice as > long > 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 (dict.__getitem__()). 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. Kent _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor