Java 似乎比 C++ 更快地执行基本算法.为什么? [英] Java seems to be executing bare-bones algorithms faster than C++. Why?
问题描述
简介:
使用两个相同的归并排序算法,我测试了 C++(使用 Visual Studios C++ 2010 express)与 Java(使用 NetBeans 7.0)的执行速度.我猜想 C++ 的执行速度至少会稍微快一点,但测试表明 C++ 的执行速度比 Java 执行慢 4 到 10 倍.我相信我已经为 C++ 设置了所有速度优化,并且我将发布作为一个版本而不是作为一个调试.为什么会出现这种速度差异?
代码:
Java:
公共类 PerformanceTest1{/*** 使用归并排序算法对数组进行排序* @param array 要排序的数组* @return 排序后的数组*/公共静态无效排序(双[]数组){if(array.length > 1){国际中心;双[]左;双[]对;int arrayPointer = 0;int leftPointer = 0;int rightPointer = 0;center = (int)Math.floor((array.length)/2.0);左 = 新双 [中心];right = new double[array.length - center];System.arraycopy(array,0,left,0,left.length);System.arraycopy(array,center,right,0,right.length);排序(左);排序(右);while((leftPointer < left.length) && (rightPointer < right.length)){if(left[leftPointer] <= right[rightPointer]){数组[数组指针] = 左[左指针];左指针 += 1;}别的{数组[数组指针] = 右[右指针];右指针 += 1;}数组指针 += 1;}if(leftPointer < left.length){System.arraycopy(left,leftPointer,array,arrayPointer,array.length - arrayPointer);}否则 if(rightPointer < right.length){System.arraycopy(right,rightPointer,array,arrayPointer,array.length - arrayPointer);}}}公共静态无效主(字符串参数[]){//要排序的元素个数int arraySize = 1000000;//创建时间变量双启动;双端;双倍持续时间;//构建数组double[] data = new double[arraySize];for(int i = 0;i < data.length;i += 1){数据[i] = Math.round(Math.random() * 10000);}//运行性能测试开始 = System.nanoTime();排序(数据);end = System.nanoTime();//输出性能结果持续时间 = (结束 - 开始)/1E9;System.out.println("持续时间:" + 持续时间);}}
C++:
#include #include 使用命名空间标准;//归并排序void sort1(double *data,int size){如果(大小> 1){国际中心;双*左;int leftSize;双*右;int rightSize;int 数据指针 = 0;int leftPointer = 0;int rightPointer = 0;center = (int)floor((size)/2.0);leftSize = 中心;left = new double[leftSize];for(int i = 0;i
观察:
对于 10000 个元素,C++ 的执行时间大约是 Java 执行时间的 4 倍.对于 100000 个元素,比例约为 7:1.对于 10000000 个元素,比例约为 10:1.超过 10000000,Java 执行完成,但 C++ 执行停止,我必须手动终止进程.
我认为您运行程序的方式可能有误.当您在 Visual C++ Express 中按 F5 时,程序在调试器下运行并且速度会慢很多.在其他版本的 Visual C++ 2010(例如我使用的 Ultimate)中,尝试按 CTRL+F5(即不调试启动)或尝试运行可执行文件本身(在 Express 中),您会看到不同之处.
我在我的机器上只做了一个修改就运行了你的程序(添加了 delete[] left; delete[] right;
以消除内存泄漏;否则它会在 32 位中耗尽内存模式!).我有一个 i7 950.公平地说,我也将相同的数组传递给 Java 中的 Arrays.sort() 和 C++ 中的 std::sort.我使用的数组大小为 10,000,000.
以下是结果(以秒为单位的时间):
<前>Java代码:7.13Java Arrays.sort:0.9332位C++ 代码:3.57C++ std::sort 0.8164位C++ 代码:2.77C++ std::sort 0.76因此,C++ 代码要快得多,即使是在 Java 和 C++ 中都高度调整的标准库,也往往对 C++ 显示出轻微的优势.
我刚刚在您的原始测试中意识到,您在调试模式下运行 C++ 代码.您应该切换到发布模式并在调试器之外运行它(正如我在我的帖子中所解释的)以获得公平的结果.
Introduction:
Using two identical mergesort algorithms, I tested the execution speed of C++ (using Visual Studios C++ 2010 express) vs Java (using NetBeans 7.0). I conjectured that the C++ execution would be at least slightly faster, but testing revealed that the C++ execution was 4 - 10 times slower than the Java execution. I believe that I have set all the speed optimisations for C++, and I am publishing as a release rather than as a debug. Why is this speed discrepancy occurring?
Code:
Java:
public class PerformanceTest1
{
/**
* Sorts the array using a merge sort algorithm
* @param array The array to be sorted
* @return The sorted array
*/
public static void sort(double[] array)
{
if(array.length > 1)
{
int centre;
double[] left;
double[] right;
int arrayPointer = 0;
int leftPointer = 0;
int rightPointer = 0;
centre = (int)Math.floor((array.length) / 2.0);
left = new double[centre];
right = new double[array.length - centre];
System.arraycopy(array,0,left,0,left.length);
System.arraycopy(array,centre,right,0,right.length);
sort(left);
sort(right);
while((leftPointer < left.length) && (rightPointer < right.length))
{
if(left[leftPointer] <= right[rightPointer])
{
array[arrayPointer] = left[leftPointer];
leftPointer += 1;
}
else
{
array[arrayPointer] = right[rightPointer];
rightPointer += 1;
}
arrayPointer += 1;
}
if(leftPointer < left.length)
{
System.arraycopy(left,leftPointer,array,arrayPointer,array.length - arrayPointer);
}
else if(rightPointer < right.length)
{
System.arraycopy(right,rightPointer,array,arrayPointer,array.length - arrayPointer);
}
}
}
public static void main(String args[])
{
//Number of elements to sort
int arraySize = 1000000;
//Create the variables for timing
double start;
double end;
double duration;
//Build array
double[] data = new double[arraySize];
for(int i = 0;i < data.length;i += 1)
{
data[i] = Math.round(Math.random() * 10000);
}
//Run performance test
start = System.nanoTime();
sort(data);
end = System.nanoTime();
//Output performance results
duration = (end - start) / 1E9;
System.out.println("Duration: " + duration);
}
}
C++:
#include <iostream>
#include <windows.h>
using namespace std;
//Mergesort
void sort1(double *data,int size)
{
if(size > 1)
{
int centre;
double *left;
int leftSize;
double *right;
int rightSize;
int dataPointer = 0;
int leftPointer = 0;
int rightPointer = 0;
centre = (int)floor((size) / 2.0);
leftSize = centre;
left = new double[leftSize];
for(int i = 0;i < leftSize;i += 1)
{
left[i] = data[i];
}
rightSize = size - leftSize;
right = new double[rightSize];
for(int i = leftSize;i < size;i += 1)
{
right[i - leftSize] = data[i];
}
sort1(left,leftSize);
sort1(right,rightSize);
while((leftPointer < leftSize) && (rightPointer < rightSize))
{
if(left[leftPointer] <= right[rightPointer])
{
data[dataPointer] = left[leftPointer];
leftPointer += 1;
}
else
{
data[dataPointer] = right[rightPointer];
rightPointer += 1;
}
dataPointer += 1;
}
if(leftPointer < leftSize)
{
for(int i = dataPointer;i < size;i += 1)
{
data[i] = left[leftPointer++];
}
}
else if(rightPointer < rightSize)
{
for(int i = dataPointer;i < size;i += 1)
{
data[i] = right[rightPointer++];
}
}
delete left;
delete right;
}
}
void main()
{
//Number of elements to sort
int arraySize = 1000000;
//Create the variables for timing
LARGE_INTEGER start; //Starting time
LARGE_INTEGER end; //Ending time
LARGE_INTEGER freq; //Rate of time update
double duration; //end - start
QueryPerformanceFrequency(&freq); //Determinine the frequency of the performance counter (high precision system timer)
//Build array
double *temp2 = new double[arraySize];
QueryPerformanceCounter(&start);
srand((int)start.QuadPart);
for(int i = 0;i < arraySize;i += 1)
{
double randVal = rand() % 10000;
temp2[i] = randVal;
}
//Run performance test
QueryPerformanceCounter(&start);
sort1(temp2,arraySize);
QueryPerformanceCounter(&end);
delete temp2;
//Output performance test results
duration = (double)(end.QuadPart - start.QuadPart) / (double)(freq.QuadPart);
cout << "Duration: " << duration << endl;
//Dramatic pause
system("pause");
}
Observations:
For 10000 elements, the C++ execution takes roughly 4 times the amount of time as the Java execution. For 100000 elements, the ratio is about 7:1. For 10000000 elements, the ratio is about 10:1. For over 10000000, the Java execution completes, but the C++ execution stalls, and I have to manually kill the process.
I think there might be a mistake in the way you ran the program. When you hit F5 in Visual C++ Express, the program is running under debugger and it will be a LOT slower. In other versions of Visual C++ 2010 (e.g. Ultimate that I use), try hitting CTRL+F5 (i.e. Start without Debugging) or try running the executable file itself (in the Express) and you see the difference.
I run your program with only one modification on my machine (added delete[] left; delete[] right;
to get rid of memory leak; otherwise it would ran out of memory in 32 bits mode!). I have an i7 950. To be fair, I also passed the same array to the Arrays.sort() in Java and to the std::sort in C++. I used an array size of 10,000,000.
Here are the results (time in seconds):
Java code: 7.13 Java Arrays.sort: 0.93 32 bits C++ code: 3.57 C++ std::sort 0.81 64 bits C++ code: 2.77 C++ std::sort 0.76
So the C++ code is much faster and even the standard library, which is highly tuned for in both Java and C++, tends to show slight advantage for C++.
Edit: I just realized in your original test, you run the C++ code in the debug mode. You should switch to the Release mode AND run it outside the debugger (as I explained in my post) to get a fair result.
这篇关于Java 似乎比 C++ 更快地执行基本算法.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!