First of all I would like to make it clear that this is not a language-X-versus-language-Y question to determine which is better.I have been using Java for a long time and I intend to keep using it. Parallel to this, I am currently learning Scala with great interest: apart from minor things that take some getting used to my impression is that I can really work very well in this language.My question is: how does software written in Scala compare to software written in Java in terms of execution speed and memory consumption?Of course, this is a difficult question to answer in general, but I would expect that higher level constructs such as pattern matching, higher-order functions, etc, introduce some overhead.However, my current experience in Scala is limited to small examples under 50 lines of code and I haven't run any benchmarks up to now. So, I have no real data.If it turned out that Scala does have some overhead wrt Java, does it make sense to have mixed Scala / Java projects, where one codes the more complex parts in Scala and the performance-critical parts in Java? Is this a common practice?EDIT 1I have run a small benchmark: build a list of integers, multiply each integer by two and put it in a new list, print the resulting list. I wrote a Java implementation (Java 6) and a Scala implementation (Scala 2.9). I have run both on Eclipse Indigo under Ubuntu 10.04.The results are comparable: 480 ms for Java and 493 ms for Scala (averaged over 100 iterations). Here are the snippets I have used.// Javapublic static void main(String[] args){    long total = 0;    final int maxCount = 100;    for (int count = 0; count < maxCount; count++)    {        final long t1 = System.currentTimeMillis();        final int max = 20000;        final List<Integer> list = new ArrayList<Integer>();        for (int index = 1; index <= max; index++)        {            list.add(index);        }        final List<Integer> doub = new ArrayList<Integer>();        for (Integer value : list)        {            doub.add(value * 2);        }        for (Integer value : doub)        {            System.out.println(value);        }        final long t2 = System.currentTimeMillis();        System.out.println(Elapsed milliseconds:  + (t2 - t1));        total += t2 - t1;    }    System.out.println(Average milliseconds:  + (total / maxCount));}// Scaladef main(args: Array[String]){    var total: Long = 0    val maxCount    = 100    for (i <- 1 to maxCount)    {        val t1   = System.currentTimeMillis()        val list = (1 to 20000) toList        val doub = list map { n: Int => 2 * n }        doub foreach ( println )        val t2 = System.currentTimeMillis()        println(Elapsed milliseconds:  + (t2 - t1))        total = total + (t2 - t1)    }    println(Average milliseconds:  + (total / maxCount))}So, in this case it seems that the Scala overhead (using range, map, lambda) is really minimal, which is not far from the information provided byWorld Engineer.Maybe there are other Scala constructs that should be used with care because they are particularly heavy to execute?EDIT 2Some of you pointed out that the println's in the inner loops take up most ofthe execution time. I have removed them and set the size of the lists to 100000 instead of 20000. The resulting average was 88 ms for Java and 49 ms for Scala.
Performance of Scala compared to Java
java;performance;scala
There's one thing that you can do concisely and efficiently in Java that you can't in Scala: enumerations. For everything else, even for constructs that are slow in Scala's library, you can get efficient versions working in Scala.So, for the most part, you don't need to add Java to your code. Even for code that uses enumeration in Java, there's often a solution in Scala that is adequate or good -- I place the exception on enumerations that have extra methods and whose int constant values are used.As for what to watch out for, here are some things.If you use the enrich my library pattern, always convert to a class. For example:// WRONG -- the implementation uses reflection when calling isWordimplicit def toIsWord(s: String) = new { def isWord = s matches [A-Za-z]+ }// RIGHTclass IsWord(s: String) { def isWord = s matches [A-Za-z]+ }implicit def toIsWord(s: String): IsWord = new IsWord(s)Be wary of collection methods -- because they are polymorphic for the most part, JVM does not optimize them. You need not avoid them, but pay attention to it on critical sections. Be aware that for in Scala is implemented through method calls and anonymous classes.If using a Java class, such as String, Array or AnyVal classes that correspond to Java primitives, prefer the methods provided by Java when alternatives exist. For example, use length on String and Array instead of size.Avoid careless use of implicit conversions, as you can find yourself using conversions by mistake instead of by design.Extend classes instead of traits. For example, if you are extending Function1, extend AbstractFunction1 instead.Use -optimise and specialization to get most of Scala.Understand what is happening: javap is your friend, and so are a bunch of Scala flags that show what's going on.Scala idioms are designed to improve correctness and make the code more concise and maintainable. They are not designed for speed, so if you need to use null instead of Option in a critical path, do so! There's a reason why Scala is multi-paradigm.Remember that the true measure of performance is running code. See this question for an example of what may happen if you ignore that rule.