|
|
|
|
|
by djur
3009 days ago
|
|
That's how it could work theoretically, but in practice lazy iterators are much less efficient due to their implementation. I benchmarked it here: https://gist.github.com/mboeh/bb480d93c71046e23816f2c24c23e3... When applied to the same amount of work, lazy iterators consume more memory and take more CPU time than the equivalent non-lazy chained iteration. There's a place for lazy iterators, but you should pretty much always profile first. If you have performance problems due to a long chain of iterators, you're always going to get better results by merging some of those iterators into a single block than by making the whole chain lazy. The best use case for lazy iterators is if you're trying to avoid having the original collection in memory (if it's being streamed from a file or the like). And at that point, if you're trying to optimize for performance, you should avoid chained iterations, period. The good news is that in the vast majority of cases this just doesn't matter. Chaining iterators works fine until you start running into performance problems. |
|