Usage of main()

Carl Banks pavlovevidence at gmail.com
Fri Sep 4 03:30:38 EDT 2009


On Sep 3, 11:55 pm, alex23 <wuwe... at gmail.com> wrote:
> Sean DiZazzo <half.ital... at gmail.com> wrote:
> > What are you using to test the scripts?  I could be completely wrong,
> > but I find it hard to believe that the second version is much (if any)
> > faster than the first.  Then again, I don't know much about the
> > internals...
>
> Sorry, Sean, unfortunately you are wrong, although it's understandable
> that you've missed this.
>
> The lookup of locally scoped references is a lot faster than that of
> global ones, primarily due to the lookup order: it first checks the
> local scope and then out through surrounding scopes _before_ the
> global scope.

Sorry, alex, unfortunately you are wrong, although it's understandable
that you've missed this.

Actually, Python knows if a variable is local, nonlocal (meaning a
local from a surrounding scope), or global at compile time, so at run
time Python attempts only one kind of lookup.

The speedup comes because local lookups are much faster.  Accessing a
local is a simple index operation, and a nonlocal is a pointer deref
or two, then an indexing.  However for global variables the object is
looked up in a dictionary.


Carl Banks



More information about the Python-list mailing list