为什么 Scala 中的对象创建速度很快?
来自序列化的此比较 JVM 上的库,看起来在 Scala 中创建对象比在 Java 中更快。不过,差异以纳秒为单位。
是否有任何真正的原因可以解释为什么在 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对象创建时间的 40 纳秒差异是 Intel Core i7 920 中的背景噪音。
假设这些数字是多次运行的平均值,则 40 纳秒仅为 0.04 微秒。假设在 Windows 7 64 位上,高性能时钟运行正常,您可能会看到 Windows 中的打嗝、月相、统计错误、测量程序错误、内存分配实现速度或其他完全不同的情况。
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 自动创建更多小对象。这使得对象创建速度平均更快,但序列化大小更大。
Scala creates more small objects automatically. This makes object creation faster on average but the serialization size larger.
更新这篇旧文章:
页面已移动,
https://github.com/eishay/jvm- Serializers/wiki
Scala 现在未列出。搜了群也没有解释为什么?
老帖子,但争论仍在继续,
Scala 比 Java 更快,直到...
我认为 Rex Kerr 帖子指的是 Scala 集合类单独保存其元素的方式。我手头上没有基准测试,但这并不是快一点(难以证明且无利可图),我已经多次进行了基准测试,速度更快。想必 Scala 程序员都知道这一点。
请参阅此处 Scala 集合 ImmutableList 中的 readObject/writeObject 方法的代码,
不可变列表的代码页
循环,循环...
快速浏览一下基准代码,
Scala 基准代码
显示了 JavaConversions._ 的使用,在不断的开发过程中,并在其中承载上述内容请注意,可能已经给 Scala 带来了轻微优势。
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.