Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How well does Scala perform compared to Java? [closed]

The Question actually says it all.

The reason behind this question is I am about to start a small side project and want to do it in Scala. I am learning scala for the past one month and now I am comfortable working with it. The scala compiler itself is pretty slow (unless you use fsc). So how well does it perform on JVM? I previously worked on groovy and I had seen sometimes over performed than java. My Question is how well scala perform on JVM compared to Java. I know scala has some very good features(FP, dynamic lang, statically typed...) but end of the day we need the performance...

like image 345
Teja Kantamneni Avatar asked Mar 19 '10 18:03

Teja Kantamneni


People also ask

Is Scala faster than Java?

According to other websites, Scala is faster than Java. Some programmers even claim that Scala is faster than Java. But the Scala compiler supports an optimisation technique called tail call recursion. This optimisation makes Scala code compile faster than Java code.

What do you like most about the Scala programming language?

The compactness of the Scala programming language is really worth noting. I can write a field even without the help of getters and setters, and that too without even bottlenecking it. Meaning, that development of the Java language runs towards compactness as well. Needless to say that Java has a few tricks up its sleeve as well.

What is the difference between Java variables and Scala variables?

Java variables are by default mutable type. Scala treated everything as an instance of the class and it is more object oriented language as compare to Java. Java is less object oriented as compare to Scala due to presence of primitives and statics.

Can Scala overpower Java in the next year?

Moreover, Scala is robust as of Java with superior capacities. Therefore, we can say that in the upcoming year, Scala can overpower the Java programming language. Where can I learn Java and Scala?


2 Answers

A majority of my work uses Scala as a high-performance language. If one really pays careful attention to performance, Scala is almost always nearly as good as Java (if not equivalent). If one is careless about e.g. object creations, it can be many times worse--as it can in Java if you use a library that is careless about object creations. (In fact, my Scala code is often faster than my Java code because I find it so much easier to make my highly optimized code convenient to use and reuse--but the Java would be as fast or faster if only I had more time and patience.)

If you want some data that demonstrates that Scala can be basically as fast as Java, check out the results on the Computer Languages Benchmark Game. (Another less useful but still interesting comparison for high-throughput multicore programming is Tim Bray's Wide Finder 2. This is less useful because the algorithm is not defined in advance, so a large portion of the differences boil down to differences in algorithm.)

like image 94
Rex Kerr Avatar answered Sep 23 '22 01:09

Rex Kerr


Scala is compiled down to byte code and is statically typed so many of the same optimizations that can be done for statically typed languages like Java (as opposed to dynamically typed languages like Groovy) can be done. So comparing Groovy to Scala is comparing apples to oranges.

Now, Java to Scala comparison:

You can expect Scala to be on par with Java in most situations. Scala can be slow if you program in it stupidly, e.g., tones of mix-ins via Traits could provide some overhead that plain Java wouldn't have.

But ...

If the Traits are actually solving a complex problem in good taste, then a solution in plain Java would have to tackle that same complexity. Who's to say the solution you write in Java using your own patterns is going to be more efficient than what you get for free in Scala (remember, the Scala compiler was written by people that are probably a better programmer than you are).

On the other hand, if you are using language features for no good reason (e.g., Integer objects when plain int primitives will do), your code will be bloated, slow, crap no matter which language you use.

In addition, consider the special class of request-response based applications that interact with a database or other I/O intensive resource. The bottle neck is not going to be the 'new' operator or virtual method invocation overhead - it will almost certainly be the I/O.

In summary, performance between Scala and Java is about the same, and shouldn't be the biggest reason you choose one over the other in 99% of cases. Since skilled human labor is more expensive than computer hardware, you are better off choosing the language that you can (or can learn to) program most efficiently in (including your teammates). If Scala lets you write one tenth the code as Java, you might gain 10X the benefit by using it. If Scala slows you down 10 times (because it's too hard to read), stick with Java!

like image 28
les2 Avatar answered Sep 23 '22 01:09

les2