why a main() function?

Diez B. Roggisch deets at nospam.web.de
Tue Sep 19 10:49:50 EDT 2006


Fredrik Lundh wrote:

> "Diez B. Roggisch" wrote:
> 
>>> There is another secondary advantage: the code inside a function runs
>>> faster (something related is true for C programs too). Usually this
>>> isn't important, but for certain programs they can go 20%+ faster.
>>
>> I totally fail to see why that should be the case - for python as well as
>> for C.
>>
>> So - can you explain that a bit more, or provide resources to read up on
>> it?
> 
> Python stores local variables in an indexed array, but globals in a
> dictionary. Looking things up by index is faster than looking them up by
> name.

Interesting. How is the index computed? I would have assumed that locals()
is somehow used, which is a dicht.

I can imagine enumerating left-hand-side names and trying to replace their
occurence with the index, falling back to the name if that is not
possible/the index isn't found. Does that come close?

Diez



More information about the Python-list mailing list