C# Java 与 .NET 性能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29933973/
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
Java vs .NET performance
提问by Yan Paulo
I got myself very surprised after I wrote this small code to compare .NET 4.5 and Java 8 performance in my computer:
在我编写了这个小代码来比较 .NET 4.5 和 Java 8 在我的计算机上的性能后,我感到非常惊讶:
class ArrayTest
{
public int[][] jagged;
public ArrayTest(int width, int height)
{
Height = height;
Width = width;
Random rng = new Random();
jagged = new int[width][];
for (int i = 0; i < height; i++)
{
jagged[i] = new int[width];
for (int j = 0; j < jagged[i][j]; j++)
{
jagged[i][j] = rng.Next(2048);
}
}
}
public int this[int i, int j]
{
get
{
return jagged[i][j];
}
set
{
jagged[i][j] = value;
}
}
public void DoMath(ArrayTest a)
{
for (int i = 0; i < Height; i++)
{
for (int j = 0; j < Width; j++)
{
this[i, j] *= a[i, j];
}
}
}
public int Height { get; private set; }
public int Width { get; private set; }
}
class Program
{
static void Main(string[] args)
{
Random rng = new Random();
const int loop = 10;
int width = 10000,
height = 10000;
ArrayTest a1 = new ArrayTest(width, height),
a2 = new ArrayTest(width, height);
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < loop; i++)
{
a1.DoMath(a2);
}
sw.Stop();
Console.WriteLine("Time taken: " + sw.ElapsedMilliseconds);
Console.ReadKey();
}
}
Here is the Java version:
这是Java版本:
public class ArrayTest {
private int width, height;
private int[][] array;
public ArrayTest(int width, int height) {
this.width = width;
this.height = height;
array = new int[height][width];
Random rng = new Random();
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
array[i][j] = rng.nextInt(2048);
}
}
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int[][] getArray() {
return array;
}
public void doMath(ArrayTest a) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
array[i][j] *= a.getArray()[i][j];
}
}
}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
final int loops = 10;
int width = 10000, height = 10000;
ArrayTest a1 = new ArrayTest(width, height),
a2 = new ArrayTest(width, height);
long start, end;
start = java.lang.System.currentTimeMillis();
for (int i = 0; i < loops; i++) {
a1.doMath(a2);
}
end = java.lang.System.currentTimeMillis();
System.out.println("Elapsed time: " + (float)(end - start));
}
}
In my computer this C# code is taking about 5200ms to run, and the Java version is taking about 2800ms(!!!). I was actually expecting that the .NET version would run faster (or at least close to) than Java, but got very surprised with this result.
在我的电脑上,这个 C# 代码大约需要 5200 毫秒才能运行,而 Java 版本大约需要 2800 毫秒(!!!)。我实际上期望 .NET 版本会比 Java 运行得更快(或至少接近),但对这个结果感到非常惊讶。
Note: I ran the .NET version compiled in release mode, outside Visual Studio.
注意:我在 Visual Studio 之外运行了在发布模式下编译的 .NET 版本。
Can somebody explain this result? Is this really right? How could I rewrite this code so that the C#.NET version gets closer to the Java one in execution speed?
有人可以解释这个结果吗?这真的对吗?我怎样才能重写这段代码,使 C#.NET 版本在执行速度上更接近 Java 版本?
[edit]
[编辑]
Well, I know this is not a really valid benchmarking or a fair comparison, but how could I rewrite this code so that the C#.NET version gets closer to the Java one in execution speed? Tried in any forms (manipulating the jagged array directly, via a getter, etc), but the test ran even slower.
好吧,我知道这不是一个真正有效的基准测试或公平的比较,但是我如何重写这段代码,以便 C#.NET 版本在执行速度上更接近 Java 版本?尝试了任何形式(直接操作锯齿状数组,通过 getter 等),但测试运行得更慢。
[edit 2]
[编辑 2]
Edited the test so that I have width and height = 500, and loop = 5000. Now I get about 6300ms for .NET version, and about 3700ms for Java version. ran the test multiple times for each version, of course.
编辑测试,使宽度和高度 = 500,循环 = 5000。现在我得到 .NET 版本大约 6300 毫秒,Java 版本大约 3700 毫秒。当然,为每个版本多次运行测试。
[edit 3]
[编辑 3]
Just wrote a similar test using flat arrays instead of 2D arrays, and this time the .NET version runs equally or even faster than Java. So is that it? C#.NET's jagged arrays are just slower than Java's multidimensional arrays?
刚刚使用平面数组而不是二维数组编写了一个类似的测试,这次 .NET 版本的运行速度与 Java 相同甚至更快。所以是这样吗?C#.NET 的锯齿状数组只是比 Java 的多维数组慢?
回答by Abhishek
回答by FoggyDay
Any time you do any kind of benchmarking or performance analysis, you need to ask a lot of questions, and take any particular result with (to paraphrase Tanenbaum), "a metric ton of salt".
任何时候进行任何类型的基准测试或性能分析时,您都需要提出很多问题,并将任何特定结果(用Tanenbaum 的话说)“一吨盐”。
HOWEVER ....
然而 ....
I copied pasted your code, and this is what I got:
我复制粘贴了你的代码,这就是我得到的:
Compiler Version Timing
-------- ------- ------
MSVS 2012 C# 5 4448.0
Eclipse Luna JRE 1.7 977.0
So the Java program ran 4.5x faster than the C# program.
因此 Java 程序的运行速度比 C# 程序快 4.5 倍。
I also ran the MSVS "Profiling Wizard":
我还运行了 MSVS“分析向导”:
https://msdn.microsoft.com/en-us/library/dd264959.aspx
https://msdn.microsoft.com/en-us/library/dd264959.aspx
As you might be able to see from the screenshot, the "Big Pig" in this profile was ArrayTest.get_item(int32, int32), which consumed NEARLY HALFof the execution time:
正如您从屏幕截图中看到的那样,此配置文件中的“大猪”是 ArrayTest.get_item(int32, int32),它消耗了近一半的执行时间:


