3 ms·
What's wrong with returning a populated slice? It's basically the same operation as returning a pre-sized, pre-filled buffered channel. Then you can "range" on
by JulianMorrison 13y ago
What's wrong with returning a populated slice? It's basically the same operation as returning a pre-sized, pre-filled buffered channel. Then you can "range" on the returned value.
The only downside is that you can't defer computation of values until they are asked for.
- yoklov 13y agoFor one thing, that will use much more memory and take more time to compute.
- JulianMorrison 13y agoDon'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.
- yoklov 13y agoSure, 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.