loop beats generator expr creating large dict!?

George Young gry at ll.mit.edu
Tue Oct 3 03:50:40 CEST 2006


[Python 2.5c2 (r25c2:51859, Sep 17 2006, 19:57:40), sparc solaris]

I am puzzled that creating large dicts with an explicit iterable of
key,value pairs seems to be slow.  I thought to save time by doing:

   palettes = dict((w,set(w)) for w in words)

instead of:

   palettes={}
   for w in words: 
      palettes[w]=set(w)

where words is a list of 200000 english words.  But, in fact,
timeit shows the generator expression takes 3.0 seconds
and the "for" loop 2.1 seconds.  Am I missing something?  

For good measure, I did an (old fashioned?!) list comprehension:

   palettes = dict([(w,set(w)) for w in words])

which took 3.1 seconds.  So it seems that, by either list
comprehension or generator expression, creating a large
dictionary all at once is slower than looping to set individual
entries.  This seems illogical.  Shouldn't dict be able to take
advantage of having all it's entries at once to create itself
faster?

-- George Young
-- 
"Are the gods not just?"  "Oh no, child.
What would become of us if they were?" (C.S. Lewis)



More information about the Python-list mailing list