Skip to main content
4 events
when toggle format what by license comment
Mar 27, 2019 at 9:21 comment added amon @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.
Mar 26, 2019 at 22:04 comment added Arseni Mourzenko @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.
Mar 26, 2019 at 21:58 comment added Ahmad F 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)...
Mar 26, 2019 at 21:52 history answered Arseni Mourzenko CC BY-SA 4.0