Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

3
  • Thanks for the answer. Honestly, I did not profile it, however I can see that using higher order functions iterates more than the standard for-loop (that's what is displayed on the playground)... Commented Mar 26, 2019 at 21:58
  • 3
    @AhmadF: what you or I see is irrelevant, because we would usually be wrong, and the profiler would always be right when it comes to finding the bottleneck. Let the machine do what it does the best; profilers are good at spotting problematic points, and you are good at deciding whether the code is readable or not, and how to make it fast enough while keeping high readability. Commented Mar 26, 2019 at 22:04
  • 1
    @AhmadF you may be underestimating the compiler: a compiler can often inline such higher-order functions and can fuse multiple loops so that the original array is only iterated once. In this case, you probably have to use array.lazy in order to make loop fusion possible (see the assembly). Note that multiple iteration passes are not always slower than a single pass. Both are O(n), and what is faster in practice often depends on subtle cache effects. Commented Mar 27, 2019 at 9:21