Re:Iterator patterns in Go
by yoklov on Sunday September 08, 2013 @11:34AM
(#6349030)
For one thing, that will use much more memory and take more time to compute.
[ Parent
| on HN ]
Re:Iterator patterns in Go
by JulianMorrison on Sunday September 08, 2013 @09:58PM
(#6351427)
Don't underestimate the time cost of coroutining (literally goroutining, and using a channel, or to-and-fro with HasNext and Next). And the unfinished generator is using memory, too. Code runs fastest when it can sit in cache, so creating a slice in a tight loop and returning it is probably optimal if the actual data you just materialized is small.
Sure, it's better than the goroutine approach, but that doesn't say much. It becomes quite suboptimal if you have a large amount of data. I also think that you're likely underestimating the cost of the additional memory allocations.