简体   繁体   中英

Why is object creation fast in Scala?

From this comparison of serialization libraries on the JVM, it looks like it is faster to create an object in Scala than in Java. The difference is in nanoseconds, though.

Is there any real reason why it would take less time to create an object in Scala, or the graph just reflects improper benchmarking or some other sort of imprecision?

40 nanosecond difference in object creation time is background noise in a Intel Core i7 920.

Assuming the numbers are an average over several runs, 40 nanoseconds is just 0.04 microseconds. Assuming on Windows 7 64-bit that the High Performance clock was functioning correctly, you're probably looking at hiccups in windows, the phase of the moon, statistical error, measuring program error, memory allocation implementation speeds, or something else entirely.

Scala creates more small objects automatically. This makes object creation faster on average but the serialization size larger.

Update on this older post:

The page has moved,

https://github.com/eishay/jvm-serializers/wiki

Scala is not listed now. Searching the group did not explain why?

Old post, but arguments continued,

Scala is faster than Java until...

I think the Rex Kerr post is referring to the way Scala collection classes save their elements individually. I don't have benchmarks to hand, but this is not a little (hard to prove and unprofitably) faster, I've benchmarked as repeatedly faster. Presumably, the Scala coders know this.

See the code for readObject/writeObject methods in the Scala collection ImmutableList here,

Code page for Immutable List

loop, loop...

A quick look at the benchmark code,

benchmark code for Scala

shows use of JavaConversions._ which, in the ongoing flux of development, and bearing the above in mind, may have given/give Scala a slight advantage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM