3 ms·
Under what circumstances are you okay with giving up 3x performance? I find the while-loop code ("Version 2") as readable and idiomatic as the one using count()
by mrdmnd 14y ago
Under what circumstances are you okay with giving up 3x performance? I find the while-loop code ("Version 2") as readable and idiomatic as the one using count(). If you can write fast Scala code (and you've already shown that you can), why not do it? Have you measured how much readability is impacted by slightly more verbose (or C-like) patterns?
Also, hi Russell!
- rcoh 14y agoIn real production code, things like this are rarely bottlenecks. The count version, frankly, is less error prone. There are a few different ways that you can easily mess up the while loop version. In places where a loop like this really is the bottleneck we'll drop in the C-style version. In general though, we find the code just kind of balloons if you use patterns like this everywhere.
- jlarocco 14y agoI have to agree with mrdmnd here. I know the old saying about premature optimization, but avoiding premature optimization doesn't mean actively choosing slower code. It just seems like sloppy programming, IMO.
- anorwell 14y agoConcise, readable code should trump performance concerns in cases where performance doesn't matter. The idiomatic version is much shorter and clearer.
- nessus42 14y agoThe equating of writing slower code to "sloppy programming" seems like sloppy reasoning to me. If it was sloppy to knowingly go with a slower solution, then using Python, for instance, or any scripting language for that matter, would always be "sloppy". It isn't. For me, Scala provides a happy middle ground between a scripting language like Python, and an "enterprise" language like Java. Scala is as nearly performant as Java--and sometimes it performs significantly better than Java due to having parallel collections, and the like--while having nearly the expressiveness of a scripting language like Python. Also, Scala's performance issues are overblown. I program in Scala professionally every day and it performs great.
- sreque 14y agoKeep 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.