4 ms·
I think the issue with syntax like this is that unless the compiler/language is smart enough, this syntax can be significantly slower than traditional for-loops
by dexwiz 5y ago
I think the issue with syntax like this is that unless the compiler/language is smart enough, this syntax can be significantly slower than traditional for-loops. For-loop blocks are often scoped differently than arbitrary closures, can capture variables differently, and have different performance overheads. Prime example is JS's Array.map being much slower than a native for-loop without optimization.
- kaba0 5y agoDepending on how flexible should it be, it can just very well be a temporary class storing the from and to value and returning it and the final ‘do’ having a completely normal for loop inside iterating from from to to. This seems to have at most a (in Java very cheap) single plus allocation (static lambdas compile to static methods so not even that allocates in itself). So I don’t see why would this base case have any significant overhead. Of course a complete stream api will have some.