loop beats generator expr creating large dict!?
bencvt at gmail.com
Tue Oct 3 07:09:08 CEST 2006
George Young wrote:
> 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:
> for w in words:
> 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?
Creating those 200,000 (w, set(w)) intermediate tuples isn't free. You
aren't doing that in for loop version. If you were:
# Slowest of all!
for w,s in ((w,set(w)) for w in words):
More information about the Python-list