…on life, the universe, and everything

Iterators vs list comprehensions

So I made a discovery today... I was profiling iterators against their list comprehension counterparts and discovered that iterators are actually slower.

Not always slower though. They're slower if you're evaluating every object in the list.

Here is my test code:

def iters():
    x = (x+1 for x in xrange(1,10000))
    y = (y/1.5 for y in x)
    z = [z**2 for z in y]

def listcomps():
    x = [x+1 for x in xrange(1,10000)]
    y = [y/1.5 for y in x]
    z = [z**2 for z in y]

>>> %timeit iters()
100 loops, best of 3: 5.38 ms per loop

>>> %timeit listcomps()
100 loops, best of 3: 4.06 ms per loop

I guess I shouldn't be surprised. There has to be some overhead to setting up the iterator, and if there were more elements, or if they were bigger (like django ORM objects), it probably wouldn't be as noticable.

I'd be interested to hear about practical applications though, so leave a comment =D

Advantages of the Iterator (even though it's slower)

  • Uses less memory, since you don't store those lists in RAM
  • May actually be faster if you don't operate on all it's elements (useful if you don't know how many you'll use)

Another note: You can't use those iterators again. Once you've cycled through the elements... that's it. You'll need to re-declare it.

The RAM saving and deferred execution will probably come in handy in my always-RAM-strapped django apps =D