gpt4 book ai didi

Java 似乎比 C++ 更快地执行基本算法。为什么?

转载 作者:可可西里 更新时间:2023-11-01 18:40:28 25 4
gpt4 key购买 nike

简介:

使用两种相同的合并排序算法,我测试了 C++(使用 Visual Studios C++ 2010 express)与 Java(使用 NetBeans 7.0)的执行速度。我推测 C++ 执行至少会稍微快一些,但测试显示 C++ 执行比 Java 执行慢 4 - 10 倍。我相信我已经为 C++ 设置了所有速度优化,并且我将作为发布而不是作为调试发布。为什么会出现这种速度差异?

代码:

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");
}

观察:

对于 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 code:            7.13Java Arrays.sort:     0.9332 bitsC++ code:             3.57C++ std::sort         0.8164 bitsC++ code:             2.77C++ std::sort         0.76

因此 C++ 代码要快得多,即使是在 Java 和 C++ 中都经过高度调整的标准库,也往往会显示出 C++ 的轻微优势。

编辑:我刚刚在您的原始测试中意识到,您在 Debug模式下运行 C++ 代码。您应该切换到 Release 模式并在调试器外部运行它(正如我在帖子中解释的那样)以获得公平的结果。

关于Java 似乎比 C++ 更快地执行基本算法。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6428444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com