|Lucas van Valckenborch's painting|
There are tons of different reasons why you may consider switching to another language, performance is just one of those. I was interested in a very basic aspect of performance: how quickly is the code compiled from the source code able to create a string.
The tested languages are:
- Java 1.7
- Scala 2.10.0
- Kotlin 0.7.271
- Groovy 2.3.0
- Clojure 1.3.10
And the results are...
Well, the single-threaded performance and simple String handling may not be a good reason to switch at the moment. As you see, java far outperformed the other languages. If you look into the bytecode generated by Scala, Groovy and Clojure, the reason is obvious. It does so many things, that it just can't perform quick.
While Kotlin performed only about half as quick as the code compiled from java, the problem is a bit harder to spot. So let me highlight the problem...
0: new #19 // class java/lang/StringBuilderThe problem is that Kotlin compiler generated code to append method accepting Object arguments, while it is known that the argument is going to be String. Should be an easy fix, I registered a bug in Kotlin's bugtracker.
4: invokespecial #23 // Method java/lang/StringBuilder."<init>":()V
7: ldc #25 // String Hello
9: invokevirtual #29 // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
13: invokevirtual #29 // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
16: ldc #31 // String !
18: invokevirtual #29 // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
21: invokevirtual #35 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
Update: I played a little with it and found where the code is generated in the compiler. The compiler with my patch generated line by line the same code as the java compiler, and therefore it performed the same.
The truth is, even javac generated suboptimal code, it could be beaten. And next time I will give it a try.
- AMD E2-1800
- Oracle java 1.7.0_45
- Fedora linux