## Thursday, April 5, 2012

### Java vs Groovy vs Scala Simple Performance Test

There are a lot of interesting discussions/comparions about the performance between these JVM languages.
To see the real differences in action myself, I am writing some simple tests using the well-known Fibonacci number.

My environment:
Mac OS X - Snow Leopard
Java 1.6.0_26
Groovy 1.8.6
Scala 2.9.1-1

I implemented the Fibonacci in the recursive way and before I started, I ran some loops to "warm up" the JVM first to make the results more reasonable.
Anyway, here are the implementations:

Java
```    public long recursiveFib(long n) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
}
return recursiveFib(n - 1) + recursiveFib(n - 2);
}
```
Groovy
```    def long recursiveFib(long n) {
if (n == 0) {
return 0
} else if (n == 1) {
return 1
}
return recursiveFib(n - 1) + recursiveFib(n - 2)
}
```
Scala
```  def recursiveFib(n: Long): Long = {
if (n == 0) {
return 0
} else if (n == 1) {
return 1
}
return recursiveFib(n - 1) + recursiveFib(n - 2)
}
```
n result Java Groovy Scala
10 55 0 (ms) 8 (ms) 13 (ms)
30 832040 14 (ms) 59 (ms) 14 (ms)
50 12586269025 153909 (ms) 285553 (ms) 156361 (ms)

From the above table, we can see that the differences increase as n increases. One interesting thing was that while running recursiveFib(10), Scala was the slowest one. Why? I am not very sure. Probably because of the JVM wasn't "warm up" enough or there were some internal Scala's stuffs going on. That's probably a separate topic to look at.
For recursiveFib(30), Groovy is like 4 times slower than Java and Scala but probably most human can't notice the difference between 14ms and 59ms.
However, when it comes to recursiveFib(50), even Groovy is just two-fold of Java and Scala, the difference is big as 150sec vs 285sec.
Overall, Scala's performance is almost as good as Java and this is probably because of the static compile time behavior of Scala while Groovy has a long way to catch up its performance with its dynamic nature.

How to improve the Groovy performance? We can run the performance bottleneck part in Java. This for sure can increase the performance overall. However, in some cases, we can actually improve the performance by just using a right solution. For this Fibonacci number, we can use for loop instead of this recursive implementation to boost the performance. I am going to test the differences again in the next blog.

1. It is a test that so "simple". Maybe we should compare performance in a scenario that needs some concurrency.

1. Yes, probably it is too simple to demonstrate for a complex calculation.
I started with this more because I used them mostly for webapp which is usually more I/O bound and less complex calculation.
That's true tho it will be more interesting and provide a more understanding in depth to compare them in a more complex way which I should probably do next. : )

2. Groovy 2.0 is out, please take a time to test.

1. Yes, it is here : )

3. Scala performance is simply great!

4. Nice blog & thanks for the share.

Checkout http://skartecedu.in for the best digital marketing training in Chennai.