Java VM 与 .NET CLR 上的基准测试性能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1108358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:51:53  来源:igfitidea点击:

Benchmarking Performance on Java VM vs .NET CLR

java.netperformancejvmbenchmarking

提问by bunn_online

Have you ever had to justify the choice over using .NET instead of Java based on performance?

您是否曾经不得不根据性能证明选择使用 .NET 而不是 Java 是合理的?

For a typical high volume transaction processing system that can perform the following operations,

对于可以执行以下操作的典型大容量事务处理系统,

  • Concurrent Database transactions
  • Mathematical computations
  • Interaction with other web services (SOAP/XML, XML-RPC)
  • 并发数据库事务
  • 数学计算
  • 与其他 Web 服务(SOAP/XML、XML-RPC)的交互

My approach would be to code benchmark tests in both Java for the JVM and C# for .NET CLR that benchmark the above operations under various levels of load and compare the results.

我的方法是在 Java 中为 JVM 编写基准测试,在 C# 中为 .NET CLR 编写基准测试,在各种负载级别下对上述操作进行基准测试并比较结果。

Language and platform preferences aside, I am interested in hearing how you would go about doing a conclusive performance comparisonbetween the Java VM and .NET CLR?

撇开语言和平台偏好不谈,我很想听听您将如何在 Java VM 和 .NET CLR 之间进行最终的性能比较

Are there any comprehensive and respected benchmarks that exist?

是否存在任何全面且受人尊重的基准?

采纳答案by JulianR

I don't have exact numbers on the efficiency of the JVM vs the CLR, but the difference, if any, is likely to be small.

我没有关于 JVM 与 CLR 效率的确切数字,但差异(如果有)可能很小。

However, on the language side, C# does have some more low level constructs than Java, which would allow for more optimization.

然而,在语言方面,C# 确实有一些比 Java 更底层的结构,这将允许更多的优化。

Constructs such as:

构造如:

  • User defined value types. Fast to allocate, no memory overhead (which is 12 bytes per reference type in both the CLR and JVM if I remember correctly). Useful for things that let themselves naturally be expressed as values, like vectors and matrices. So mathematical operations. Combine this with refand outto avoid excessive copying of these large value types.

  • Unsafe blocks of code that allow a little more 'close to the metal' optimization. For example, while the CLR and JVM can avoid array bounds checks in some situations, in a lot of cases, they can't and every array access requires a check whether or not the index is still within bounds of the array. Using unsafe code here allows you to access the memory of the array directly with pointers and circumvent any bounds checks. This can mean a significant saving. And on the very low level side, there's also stackallocwhich allows you to allocate arrays directly on the stack, whereas a normal array is allocated on the heap, which is slower, but also more convenient. I personally don't know any practical applications of stackalloc.

  • True generics, unlike the type erasing generics of Java, avoiding unneeded casting and boxing. But if this is a problem in your Java program, it can easily be solved with some extra work (switching from for example a ArrayList<Integer>to a custom type that internally uses an int[]buffer.)

  • 用户定义的值类型。分配速度快,没​​有内存开销(如果我没记错的话,在 CLR 和 JVM 中每个引用类型都是 12 个字节)。对于让自己自然地表示为值的事物很有用,例如向量和矩阵。所以数学运算。将此与refout避免过度复制这些大值类型。

  • 不安全的代码块,允许更多的“接近金属”优化。例如,虽然 CLR 和 JVM 在某些情况下可以避免数组边界检查,但在很多情况下,它们不能,并且每次访问数组都需要检查索引是否仍在数组范围内。在这里使用不安全代码允许您直接使用指针访问数组的内存并绕过任何边界检查。这可能意味着可观的节省。而在非常低的层面上,也有stackalloc它允许您直接在堆栈上分配数组,而在堆上分配普通数组,这更慢,但也更方便。我个人不知道stackalloc.

  • 真正的泛型,与 Java 的类型擦除泛型不同,避免了不必要的强制转换和装箱。但是,如果这是您的 Java 程序中的问题,则可以通过一些额外的工作轻松解决(例如从 a 切换ArrayList<Integer>到内部使用int[]缓冲区的自定义类型。)

This all seems biased towards C# and I do think C# has better low level language constructs available that can help with performance. However, I doubt these differences really matter (and they might not even apply in your case, using pointers gains you nothing if all you do is database access, where Java might be faster) if the choice impedes you in some other way (like going cross platform). Go for correctness, the platform that matches your requirements, rather than minor performance differences.

这一切似乎都偏向于 C#,我确实认为 C# 有更好的低级语言结构可以帮助提高性能。但是,我怀疑这些差异是否真的很重要(它们甚至可能不适用于您的情况,如果您所做的只是数据库访问,则使用指针不会给您带来任何好处,而 Java 可能会更快)如果选择以其他方式阻碍您(例如跨平台)。追求正确性,即符合您要求的平台,而不是细微的性能差异。

回答by user133018

Yeah, there is the benchmark game it is pretty comprehensive and allows you to compare many different things.

是的,有一个基准游戏,它非常全面,可以让您比较许多不同的东西。

http://shootout.alioth.debian.org/

http://shootout.alioth.debian.org/

The only thing is it uses mono instead of visual studio but the performance difference between the two is now very small.

唯一的问题是它使用单声道而不是视觉工作室,但现在两者之间的性能差异非常小。

in general java is usually slightly faster(depending on what you are doing) but uses a much larger memory footprint and they both are about the same source size.

一般来说,java 通常稍微快一点(取决于你在做什么),但使用更大的内存占用,而且它们的源代码大小大致相同。

回答by Spence

FYI you are specifically not allowed to use the .NET framework code for any form of benchmarking without contacting Microsoft first and getting their approval.

仅供参考,您不得在未先联系 Microsoft 并获得他们的批准的情况下,将 .NET 框架代码用于任何形式的基准测试。

If you did want to publish something I thought I'd let you know.

如果你确实想发表一些东西,我想我会告诉你的。

From memory MS did some book store, pet store thing that Java did first to show off how their software could work more effectively at the same task. I'm trying to think of it.

从记忆中 MS 做了一些书店、宠物店的事情,Java 首先做了这些事情,以展示他们的软件如何在相同的任务中更有效地工作。我正在努力思考。

.NET Pet Store

.NET 宠物商店

回答by pero

Benchmarks... You can prove almost every thing with test that suites your need.
Except Chuck Norris. Chuck Norris is slower and faster than X if he so chooses.

基准测试...您可以通过适合您需要的测试来证明几乎所有事情。
除了查克·诺里斯。如果他愿意的话,Chuck Norris 比 X 越来越慢。

Another point. Let's say you get to conclusion that Java is faster. Would that imply that 2 years from now it will still be faster ?

还有一点。假设您得出结论,Java 更快。这是否意味着 2 年后它仍然会更快?

If Java is 5% faster and .NET is 10% easier to work with what would you choose?
There are many factors and performance is just one of them. And if differences are small (I think they are) it probably is not the most important one.

如果 Java 快 5%,.NET 使用起来容易 10%,你会选择什么?
有很多因素,性能只是其中之一。如果差异很小(我认为它们很小),它可能不是最重要的。

Unless you are building something that is very performance critical.

除非您正在构建对性能非常关键的东西。

回答by Forer

I have also wondered which would give better performance. But have no idea about how I might go about performing bench mark test for this.

我也想知道哪个会提供更好的性能。但不知道我将如何为此执行基准测试。

So good question - hopefully we will all get some guidance here.

这么好的问题 - 希望我们都能在这里得到一些指导。

I would guess this benchmark testing would need to be a "discount" benchmark testing approach (easy to setup & run by 1 developer)?

我猜这个基准测试需要是一种“折扣”基准测试方法(易于由 1 个开发人员设置和运行)?

I anyone has this kind of info it would be great. I'm often asked to evaluate technologies on my own within short time scales.

我任何人都有这种信息,那就太好了。我经常被要求在短时间内自行评估技术。

Nice one bunn_online!

不错的一个bunn_online!

回答by dmihailescu

a conclusive performance comparison between the Java VM and .Net CLRis a pipe dream. You can always make a performance test that will make one look better than the other since engineering a complex system always involves compromises. If you narrow down the type of benchmarks to runtime speed and memory you might find articles like this: http://www.codeproject.com/KB/dotnet/RuntimePerformance.aspxthat is by no means the end of the debate.

Java VM 和 .Net CLR 之间决定性的性能比较是一个白日梦。您总是可以进行性能测试,使一个看起来比另一个更好,因为设计一个复杂的系统总是需要妥协。如果您将基准测试的类型缩小到运行时速度和内存,您可能会发现这样的文章:http: //www.codeproject.com/KB/dotnet/RuntimePerformance.aspx,这绝不是争论的结束。

回答by Jon Harrop

Language and platform preferences aside, I am interested in hearing how you would go about doing a conclusive performance comparison between the Java VM and .Net CLR?

除了语言和平台偏好之外,我有兴趣了解您将如何在 Java VM 和 .Net CLR 之间进行最终的性能比较?

I would write a suite of benchmarks designed to let you compare various different characteristics of the two VMs and their standard libraries. My expertise is in technical computing so I would recommend the following:

我会编写一套基准测试,旨在让您比较两个 VM 及其标准库的各种不同特性。我的专长是技术计算,所以我会推荐以下内容:

  • In register integer arithmetic, e.g. Fibonacci.
  • In register floating point arithmetic, e.g. mandelbrot.
  • Array iteration, e.g. FFT.
  • Allocation, e.g. purely functional red-black trees.
  • Hash tables with int or float keys and values.
  • Hash tables with string keys and values.
  • Strings.
  • Regular expressions.
  • File IO.
  • 在寄存器整数运算中,例如 Fibonacci。
  • 在寄存器浮点运算中,例如 mandelbrot。
  • 数组迭代,例如 FFT。
  • 分配,例如纯功能性红黑树。
  • 带有 int 或 float 键和值的哈希表。
  • 带有字符串键和值的哈希表。
  • 字符串。
  • 常用表达。
  • 文件 IO。

Perhaps you can come up with something similar for databases and web services.

也许您可以为数据库和 Web 服务想出类似的方法。

Don't forget that the languages sitting on top of the CLR have quite different properties. For example, inlinein the F# language lets you automate optimizations that can give huge performance gains over C#. Conversely, gotoin C# lets you do some things more efficiently than is possible in F# and the optimizations on structs were more effective in C# than F# (last I looked).

不要忘记位于 CLR 之上的语言具有完全不同的属性。例如,inline在 F# 语言中,您可以自动进行优化,从而比 C# 获得巨大的性能提升。相反,goto在 C# 中,您可以比在 F# 中更有效地做一些事情,并且在 C# 中对结构的优化比 F# 更有效(我最后一次查看)。

Are there any comprehensive and respected benchmarks that exist?

是否存在任何全面且受人尊重的基准?

No but there are lots of scattered benchmarks that focus on the outliers because they are more interesting. For example, this blog postdescribes why a simple generic hash table benchmark can be 17× faster in F# on .NET than in Java on the JVM. In that case the reason was that value types and reified generics make it possible to write a much more efficient generic hash table implementation on .NET than on the JVM.

不,但是有很多分散的基准测试专注于异常值,因为它们更有趣。例如,这篇博文描述了为什么一个简单的通用哈希表基准测试在 .NET 上的 F# 中比在 JVM 上的 Java 中快 17 倍。在这种情况下,原因是值类型和具体泛型使得在 .NET 上编写比在 JVM 上更高效的通用哈希表实现成为可能。