使用两个相同的mergesort算法,我测试了C++(使用Visual Studios C++ 2010 express)和Java(使用NetBeans 7.0)的执行速度.我推测C++执行至少会稍微快一点,但测试显示C++执行速度比Java执行慢4到10倍.我相信我已经为C++设置了所有速度优化,而且我发布的是发布而不是调试.为什么会出现这种速度差异?
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]; …Run Code Online (Sandbox Code Playgroud)