3 ms·
Keep in mind that the while loop is 3x faster because the loop itself is doing a trivial amount of work. As your loops get larger, call functions that do more w
by sreque 14y ago
Keep in mind that the while loop is 3x faster because the loop itself is doing a trivial amount of work. As your loops get larger, call functions that do more work, or operate on objects instead of primitives, the overhead of using idiomatic Scala drops considerably.
Another important point: the idiomatic Scala version is one concise, purely functional line. The second version on the other hand is 6 lines and involves mutation. If you were to embed it in another part of the program in a purely functional way, you'd have to wrap it in curly braces and add an extra expression to return the final value of x, upping it to 9 lines. Claim what you want about readability, but lines of code impose a cognitive burden, both in reading them and scrolling through them. I'd much rather express an idea in one line of pure code if possible than with 6-9 lines of code that modifies state.