os.listdir
Duncan Booth
duncan at NOSPAMrcp.co.uk
Fri Sep 12 04:44:19 EDT 2003
"Terry Reedy" <tjreedy at udel.edu> wrote in news:wSmdnYp3_9zFSf2iU-
KYvA at comcast.com:
>
> "Duncan Booth" <duncan at NOSPAMrcp.co.uk> wrote in message
>> In C Python, all dictionaries have an initial size of 8 elements.
> There is
>> no way to override this (except recompiling!)
>
> In you initialize a dict from a list of, say, 1000 (key,value) pairs,
> does it really disregard list size? at least after first 2/3rds
> fillup?
>
Yes. Very roughly speaking dict.__init__ looks like this (except in C):
def __init__(self, dict, **kw):
if hasattr(dict, 'keys'):
self.update(dict)
else:
for item in iter(dict):
fast = tuple_or_list(item)
assert len(fast)==2
self[fast[0]] = fast[1]
The important point being that the code accepts an iterator rather than a
list, so it doesn't know how many items will be returned. Of course there
is nothing to stop someone adding a check whether the argument is really a
list, and if so preallocating the size, but someone would have to evaluate
whether the increased complexity, and longer runtime for smaller
dictionaries justified the gain.
There's another twist to this:
d = dict(aList)
At some point you have to create 'aList', and that too will start off small
and double in size as it grows.
Even if you just write a constant dictionary:
d = { 1:1, 2:2 }
the bytecode for this creates an empty dictionary, then evaluates each key,
value pair and stores it in the dictionary.
--
Duncan Booth duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
More information about the Python-list
mailing list