Monday, December 01, 2008

The argument is that it should not be that case since Scala compiles to Java class files and runs on the same JVM/JIT, hence they are actually identical.

In my other post I claimed that for the benchmark I wrote, Scala object creation is faster. Actually, it took about 323 nano to create the java object set and only 221 nano to create the scala object set. I.e. it took about 46% more time to create the objects in Java then it would in Scala. Its pretty significant.

The code is out there so I urge you to check it out and prove me wrong.

The code as you see is almost the same. There is a minor difference with param order in the contractors, should not matter a bit, and the reference to a Scala enum Size.LARGE() which is not a real Java enum. The hidden nugget in the story is the usage of lists in Scala and Java. In Scala lists are immutable so you throw the old list once you added a new element to it. As Itay mentioned, the object immutability might makes the implementation faster.You can also compare the Scala class set and the Java class Set and check out the benchmark runner. As expected from code I write for fun after midnight, it is fully documented for those of you who can read Java and Scala.

6
comments:

Well, this doesn't show that object creation in Scala is faster than Java. At best it says that scala.List is faster than java.util.ArrayList for this particular benchmark. However, I haven't looked at the actual benchmark runner, so there could be many other factors at play (micro-benchmarking in the JVM is hard and it's easy to make mistakes).

Thanks @jau I'll fix it and update the results.@Ismael I agree, there is much more going on there then plain old object creation. I did worm up the JIT and tried to avoid GC, but you're right about micro benchmarking. In the Java side I counted about five object creation, not including a couple of ArrayList internal objects. The strings are interned by the JVM so I didn't count them. On that machine it takes about 7nano to create an object hence only ~60n out of 323n where spent on object creation. The rest of 260n where spent on the rest (methods, passing references etc). I'll try removing the extra if(), but I guess its insignificant. Arguably Scala actually creates more objects since each list.add() creates a new list and every primitive is an object though is probably compiled to a primitive if there is no reason to make an object out of it.

Also note that not all objects have the same initialisation costs, so just counting the number of objects is not good enough. An ArrayList creates an internal array with 10 elements if you invoke the default constructor, for example.

The only way to compare would be to create the same classes in Java and Scala and then benchmark object creation. You will not find a difference then because the bytecode will be very similar (if not the same).