Re: [Tutor] loop performance in global namespace (python-2.6.1)

Discussion in 'Python' started by spir, Mar 12, 2009.

  1. spir

    spir Guest

    Le Thu, 12 Mar 2009 11:13:33 -0400,
    Kent Johnson <> 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.
    Also, are locals really stored in an array? How does lookup then proceed? Is it a kind of (name,ref) sequence?

    Denis
    ------
    la vita e estrany
    spir, Mar 12, 2009
    #1
    1. Advertising

  2. spir

    John Machin Guest

    Re: loop performance in global namespace (python-2.6.1)

    On Mar 13, 2:41 am, spir <> wrote:
    > Le Thu, 12 Mar 2009 11:13:33 -0400,
    > Kent Johnson <> 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.
    John Machin, Mar 12, 2009
    #2
    1. Advertising

  3. spir

    Lie Ryan Guest

    Re: loop performance in global namespace (python-2.6.1)

    John Machin wrote:
    > On Mar 13, 2:41 am, spir <> wrote:
    >> Le Thu, 12 Mar 2009 11:13:33 -0400,
    >> Kent Johnson <> 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'
    Lie Ryan, Mar 13, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dick Moores

    [Tutor] New Python book

    Dick Moores, Oct 5, 2005, in forum: Python
    Replies:
    1
    Views:
    295
    hrh1818
    Oct 6, 2005
  2. Shawn Milochik

    Re: [Tutor] Python Book Recommendations

    Shawn Milochik, Aug 15, 2007, in forum: Python
    Replies:
    7
    Views:
    348
    Shawn Milochik
    Aug 16, 2007
  3. hong2221

    Looking for a Python tutor

    hong2221, Nov 27, 2007, in forum: Python
    Replies:
    1
    Views:
    286
    Arnaud Delobelle
    Nov 27, 2007
  4. Poor Yorick
    Replies:
    0
    Views:
    239
    Poor Yorick
    Mar 12, 2009
  5. Isaac Won
    Replies:
    9
    Views:
    364
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page