Performance Advantages to Iterators?
What (if any) performance advantages are offered by using iterators. It seems like the 'Right Way' to solve many problems, but does it create faster/more memory-conscious code? I'm thinking specifically in Python, but don't restrict answers to just that.
Solution 1:
There's actually a very good mail on the python mailing list about this: Iterators vs Lists. It's a bit dated (from 2003), but as far as I know, it's still valid.
Here's the summary:
For small datasets, iterator and list based approaches have similar performance. For larger datasets, iterators save both time and space.
What I would draw from it is this: iterators are to be preferred over loading data into a list if possible. But unless you have a big dataset, don't contort your code to make something that should fit in a list to work with an iterator.
Solution 2:
Iterators will be faster and have better memory efficiency. Just think of an example of range(1000)
vs xrange(1000)
. (This has been changed in 3.0, range
is now an iterator.) With range
you pre-build your list, but xrange
is an iterator and yields the next item when needed instead.
The performance difference isn't great on small things, but as soon as you start cranking them out getting larger and larger sets of information you'll notice it quite quickly. Also, not just having to generate and then step through, you will be consuming extra memory for your pre-built item whereas with the iterator, only 1 item at a time gets made.