[Tutor] Naming variables

spir denis.spir at gmail.com
Sun Jan 19 15:21:10 CET 2014


On 01/19/2014 02:59 PM, Mark Lawrence wrote:
> On 19/01/2014 13:23, spir wrote:
>> On 01/18/2014 07:20 PM, Pierre Dagenais wrote:
>>> Hello,
>>>
>>> I wish to fill a list called years[] with a hundred lists called
>>> year1900[], year1901[], year1902[], ..., year1999[]. That is too much
>>> typing of course. Any way of doing this in a loop? I've tried stuff like
>>> ("year" + str(1900)) = [0,0] but nothing works.
>>> Any solution?
>>>
>>> Thank you,
>>>
>>> PierreD.
>>
>> I think Danny & Wiktor's solutions are the right ones. Danny's is faster
>> a simpler (because of direct array indexing), but does not allow giving
>> the true year "names" (actually numbers). Wiktor more correctly matches
>> you expectation bt is a bit slower because we're searching individual
>> years in a dict.
>>
>
> I suspect that the speed difference between accessing a list by direct indexing
> and looking up something in a dict is negligible.  I'll happily leave Steven
> D'Aprano to supply us with the actual figures from the timeit module :)

It's 2-3 times slower, I guess (measures in other langs, including lua & 
hand-implemented in C, but not in python). The task is accessing an entry which 
key is a plain natural number, meaning a sparse array. It is typically 
implemented as a "mod table" (my term), meaning a hash table without hashing 
since keys already are natural numbers: only remains the modulo (actually just a 
mask since the base is a power of 2). The overhead is consistently 2-3 times, 
but as you say is often neglectable in practice since time remains small 
compared to whatever one does with data, once accessed.

Lua for this reason nevertheless optimises function local scopes by getting rid 
of names (unless one explicitely uses them, sort of metaprogramming) and 
replacing them with indexes in a plain array (actually kind of custom call 
stack). This is as said 2-3 times faster than access of globals (which names 
remain, but *interned* thus we have a sparse array, and the scope is the Lua 
version of a dict).

I guess (not sure) python optimises access of dicts used as scopes (also of 
object attributes) by interning id-strings and thus beeing able to replace them 
by hash values already computed once for interning, or other numeric codes, as 
keys in dicts. Thus, we fall back to the case of a sparse array as above (but in 
this case names are still there and accessible in the string pool).
[This is my preferred method, despite the additional complication of a string 
pool, and additional weight of the numeric key in strings. And once we have the 
machinary in place, it can be used for other purposes, like interning small user 
strings or, as in python, object attr names.]

Denis


More information about the Tutor mailing list