loop performance in global namespace (python-2.6.1)

Lie Ryan lie.1296 at gmail.com
Fri Mar 13 01:42:37 EDT 2009


John Machin wrote:
> On Mar 13, 2:41 am, spir <denis.s... at free.fr> wrote:
>> Le Thu, 12 Mar 2009 11:13:33 -0400,
>> Kent Johnson <ken... at tds.net> s'exprima ainsi:
>>
>>> 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__()).
>> ? I thought this was mainly because a name has first to be searched (unsuccessfully) locally before a global lookup is launched.
> 
> No; for locals, the name resolution is done at COMPILE time.
> 
>> Also, are locals really stored in an array?
> 
> Yes. You seem surprised ... the technique of locals (and args) being
> referenced at run time by a constant offset from a frame pointer
> register has been around for at least 40 years.
> 
>> How does lookup then proceed? Is it a kind of (name,ref) sequence?
> 
> There is no name lookup at run time. The values of locals (and args)
> are obtained by simple array access.
> 

Maybe you need a bit more clarification:

def foo():
     a = 10
     b = 15
     c = a + b
     d = 'somewhat'

is (sort of) turned to:

def foo():
     locals = [None, None, None, None]
     locals[0] = 10
     locals[1] = 15
     locals[2] = locals[0] + locals[1]
     locals[3] = 'somewhat'



More information about the Python-list mailing list