Writing Performant Scala
sumologic.com
sumologic.com
Also, hi Russell!
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.
In general though, we find the code just kind of balloons if you use patterns like this everywhere.
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.
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.
Understanding immutable strings aren't at the same level. People concatenate immutable strings all the time in Java/Python, but that is despite the documentation mentioning immutability and proper concatenation method(StringBuffer followed by toString, list.append() followed by " ".join(list))
What-do-you-know, Scala has documentation too[1]! And it mentions that appending to a List is O(n).
[1] http://www.scala-lang.org/api/current/scala/collection/immut...
It takes a long time for a new general purpose programming language to displace existing general purpose programming languages if it doesn't fill a very particular niche for which there is a great demand.
Re performance, I recently implemented an algorithm in Scala that performs quite well. It didn't at first, but it was very easy for me to profile it using Java profiling tools and to tighten a couple of loops where most of the CPU was being consumed. I don't have any deep knowledge of Scala collections, but I know how to use a profiler. I don't think the initial implementation would have performed any better if it had been written in Java.
Once I had gotten things speedy enough, I was further able to speed everything up by 4X just by changing a collection to the parallel version of the collection. Then all four cores could crunch on it. If I had 8 cores, it would have no doubt run 8X faster. That parallelization came from tweaking only one line of code! I would have never achieved this 4X speedup in Java.
http://boulter.com/blog/2004/08/19/performant-is-not-a-word/