Quicksorting with Java, Scala and Groovy

I found this older post comparing performance of Java, Scala and Groovy on the Quicksort algorithm. I have cleaned all source files a bit, so they all have similar/same variable and method names. I also added “best of ten” functionality to get past VM warming up and other irregularities. In original post I was quite sceptical about the time it took to process, it was way too quick - 45ms and 56ms. I know it was probably because of Groovy, but it in my opinion ruined comparison of Java and Scala, because of such short time intervals combined with no VM heating up.

Warning

I confess, I know almost nothing about Groovy, only things I did to the source was the renaming and adding “best of ten”. Also note that all examples have been written to quite closely match the Java version. I’m not sure about Groovy, but I would definitely write the Scala version a lot differently. And if the performance in real world project would be a huge problem, I would just grab some library - after all why crafting an oval wheel yourself when you can get a perfectly round wheel from store instantly and for free.

How?

As I stated, one run of test program comprise ten iterations of Quicksort and picking the fastest one. On top of this every run was done three times, following values are averages.

I want facts!

Here you go, some raw cooked data for you:

Language / Samples / [ms] 2000000 4000000 8000000 16000000 32000000
Java 32,33 70,33 140,67 302,67 647,00
Scala 29,00 61,67 236,33 496,00 1072,67
Groovy 4240,67 8483,00 17455,33 37928,67 74337,33
Scala to Java -10,31% -12,32% 68,01% 63,88% 65,79%
Groovy to Java 13015,46% 11961,14% 12309,00% 12431,50% 11389,54%

I also created some pictures! … I mean diagrams. First one is a bit useless since we only see how Groovy is unbelievably slow.

Chart with all languages

Groovy, what the hell?

I don’t know if an author of the benchmark source code in Groovy bungled it, but if not then I can’t understand why would anyone use that language. The price is too high - over 10 times slower than Java, you are far better off with Scala which is slower “only” by half and on smaller data it is even faster than Java. With Scala you are getting many little tools, speed and if you want you can start experimenting with heavy stuff like hardcore functional programming in Scalaz.

Let’s try it again, this time without sluggish Groovy. Here we go:

Chart with Java and Scala

You can see that till 4 millions of items Scala wins by ~ ten percent, but then the card flips and Java is the strongman leaving Scala behind with score of being slower by two thirds than Java.

Conclusion

If this slowness of Groovy is reality, and I did see other benchmarks supporting this, then I would not recommend using it in anything other than simple scripts. It is definitely not “better-Java” in a sense that you can easily switch and start writing your production code in Groovy, because the performance impact is huge - Groovy is over 10 times slower than Java (at least according to this benchmark).

While I agree you won’t need that often so much power like this benchmark demands, I feel that Groovy is too much slower. If you want to maintain most of the performance while getting more concise and powerful language than Java then I can recommend you Scala (you can find some examples of code and features here).


Where can I get the sources?

You can find everything at https://github.com/mnn/quicksort-benchmark. It contains helper scripts, so you can easily try it yourself. Just ./compile.sh and then ./bench.sh (or if you are on MS platform compile.bat followed by bench.bat, but you will need properly set up mingw or some similar environment). Dependencies are quite obvious - JDK, Scala and Groovy.

Other details

Compiler versions

  • Java 1.8.0_73
  • Scala 2.11.7
  • Groovy 2.4.6

Testing computer

I don’t think it is too much important, but I am appending short info about test PC.

  • Windows 10 x64
  • Intel Core i5-4460
  • SSD
  • DDR3 800MHz

Post release note

I have been contacted by some Groovy fan and it seems you can make the Groovy code running much faster, almost as fast as Java (code). But there is a big catch, you have to sacrifice dynamic typing (and add a ton of boiler-plate). In that case, I wonder, what is a point of using Groovy with static types when I could grab Scala with superior type system and many more language features?