

Writing Performant Scala  - jakozaur
http://www.sumologic.com/blog/technology/3-tips-for-writing-performant-scala

======
mrdmnd
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
In 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
I 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
Concise, readable code should trump performance concerns in cases where
performance doesn't matter. The idiomatic version is much shorter and clearer.

------
dkhenry
Its does annoy me that lazy evaluation is not the default like it is in
Haskell for much of the collections classes, but Once you figure out its
there, man does it make things faster.

------
eta_carinae
The fact that it takes so much intimate knowledge of how the collections are
implemented to get some decent performances in Scala is probably one of the
many reasons why this language hasn't taken off.

~~~
monkeyfacebag
I agree in principle that one shouldn't _have to_ understand the
implementation of collections to use them effectively, but these limitations
are by no means exclusive to Scala. Haskell's lists, difference lists and seqs
work exactly the same way as Scala's list collections. Iteratively
concatenating strings in Python will murder your performance. Most (all?)
sufficiently complex systems involve tradeoffs.

~~~
irahul
> Iteratively concatenating strings in Python will murder your performance.

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))

~~~
monkeyfacebag
> despite the documentation mentioning immutability and proper concatenation
> method

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...](http://www.scala-
lang.org/api/current/scala/collection/immutable/List.html)

------
dons
Remember folks : "performant" is not a word.

<http://boulter.com/blog/2004/08/19/performant-is-not-a-word/>

~~~
jdonaldson
"performant" may become legitimate soon, but it seems destined to be another
tech buzz/weasel word.

------
carterschonwald
So basically the recommendations boil down to "pretend youre using Haskell a
work" :-)

