On Fri, Oct 9, 2009 at 1:21 AM, wesley chun <wes...@gmail.com> wrote:
> [generator expressions] are > "lazy" because you iterate over the values one at a time instead of > creating the entire list. they are slightly slower but save memory. I don't think you can make a blanket statement comparing speed of list comp vs genexp. For example: kent $ py -m timeit 'sum(x for x in xrange(1,1000) if x%3==0 or x%5==0)' 1000 loops, best of 3: 295 usec per loop kent $ py -m timeit 'sum([x for x in xrange(1,1000) if x%3==0 or x%5==0])' 1000 loops, best of 3: 281 usec per loop Here list comp is a bit faster. kent $ py -m timeit 'sum(x for x in xrange(1,10000) if x%3==0 or x%5==0)' 100 loops, best of 3: 2.83 msec per loop kent $ py -m timeit 'sum([x for x in xrange(1,10000) if x%3==0 or x%5==0])' 100 loops, best of 3: 2.85 msec per loop Here they are neck and neck. kent $ py -m timeit 'sum(x for x in xrange(1,100000) if x%3==0 or x%5==0)' 10 loops, best of 3: 28.9 msec per loop kent $ py -m timeit 'sum([x for x in xrange(1,100000) if x%3==0 or x%5==0])' 10 loops, best of 3: 29.4 msec per loop genexp pulls in to the lead. Kent _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor