gpt4 book ai didi

java - 为什么 Java 似乎比 C++ 执行得更快 - 第 2 部分

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:52:42 27 4
gpt4 key购买 nike

<分区>

简介

这是我之前提出的问题的后续问题:Java seems to be executing bare-bones algorithms faster than C++. Why? .通过那篇文章,我了解到一些重要的事情:

  1. 我没有使用 Ctrl + F5 在 Visual Studios C++ Express 上编译和运行 C++ 代码,这导致调试减慢了代码执行速度。
  2. 在处理数据数组方面, vector 与指针一样好(如果不是更好的话)。
  3. 我的 C++ 很糟糕。 ^_^
  4. 更好的执行时间测试是迭代,而不是递归。

我尝试编写一个更简单的程序,它不使用指针(或 Java 中的等效数组),并且在执行过程中非常简单。即使这样,Java 的执行速度也比 C++ 的执行速度快。我做错了什么?

代码:

Java:

 public class PerformanceTest2
{
public static void main(String args[])
{
//Number of iterations
double iterations = 1E8;
double temp;

//Create the variables for timing
double start;
double end;
double duration; //end - start

//Run performance test
System.out.println("Start");
start = System.nanoTime();
for(double i = 0;i < iterations;i += 1)
{
//Overhead and display
temp = Math.log10(i);
if(Math.round(temp) == temp)
{
System.out.println(temp);
}
}
end = System.nanoTime();
System.out.println("End");

//Output performance test results
duration = (end - start) / 1E9;
System.out.println("Duration: " + duration);
}
}

C++:

#include <iostream>
#include <cmath>
#include <windows.h>
using namespace std;

double round(double value)
{
return floor(0.5 + value);
}
void main()
{
//Number of iterations
double iterations = 1E8;
double temp;

//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)

//Run performance test
cout << "Start" << endl;
QueryPerformanceCounter(&start);
for(double i = 0;i < iterations;i += 1)
{
//Overhead and display
temp = log10(i);
if(round(temp) == temp)
{
cout << temp << endl;
}
}
QueryPerformanceCounter(&end);
cout << "End" << endl;

//Output performance test results
duration = (double)(end.QuadPart - start.QuadPart) / (double)(freq.QuadPart);
cout << "Duration: " << duration << endl;

//Dramatic pause
system("pause");
}

观察:

对于 1E8 次迭代:

C++ 执行 = 6.45 秒

Java 执行 = 4.64 秒

更新:

根据 Visual Studios,我的 C++ 命令行参数是:

/Zi /nologo /W3 /WX- /O2 /Ob2 /Oi /Ot /Oy /GL /D "_MBCS" /Gm- /EHsc /GS /Gy /fp:precise /Zc:wchar_t /Zc:forScope /Fp"Release\C++.pch" /Fa"Release\" /Fo"Release\" /Fd"Release\vc100.pdb" /Gd /analyze- /errorReport:queue

更新 2:

我用新的 round 函数更改了 c++ 代码,并更新了执行时间。

更新 3:

感谢 Steve Townsend 和 Loduwijk,我找到了问题的答案。将我的代码编译成程序集并对其进行评估后,我发现 C++ 程序集创建的内存移动比 Java 程序集多得多。这是因为我的 JDK 使用的是 x64 编译器,而我的 Visual Studio Express C++ 不能使用 x64 架构,因此速度本来就慢。因此,我安装了 Windows SDK 7.1,并使用这些编译器来编译我的代码(在发行版中,使用 ctrl + F5)。目前的时间比例是:

C++:~2.2 秒 Java:~4.6 秒

现在我可以用 C++ 编译我的所有代码,并最终获得我的算法所需的速度。 :)

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