VS 2017&;2019年运行c&x2B+;真慢 我在Python中开始了一个小程序,但是花了很长时间才完成,所以我切换到C++。我以前没有使用这种特定语言的经验(虽然用c语言编写了很多代码),我是从一个web编辑器开始的: 我的C++代码是: clock_t start, end; /* Recording the starting clock tick.*/ start = clock(); int R = 0; int x = 0; for (R = 6; R <= 10000; R = R + 2) { int X_min = ceil(0.5 * sqrt(2) * R); int N_pairs = 0; for (x = X_min; x < R; x++) { float y = sqrt(pow(R, 2) - pow(x, 2)); if (rint(y) == y) { N_pairs = N_pairs + 1; } } if (N_pairs >= 4) { //cout << R << ", " << N_pairs; //cout << "\n"; } } end = clock(); //Calculating total time taken by the program. double time_taken = double(end - start) / double(CLOCKS_PER_SEC); cout << "Time taken by program is : " << time_taken; cout << " sec " << endl; //cout << "1" << "|" << "2" << "|" << "3 \n"; //cout << "4" << "|" << "5" << "|" << "6 \n"; //cout << "7" << "|" << "8" << "|" << "9 \n"; 时钟开始、结束; /*记录开始的时钟滴答声*/ 开始=时钟(); int R=0; int x=0; 对于(R=6;R=4){ //cout

VS 2017&;2019年运行c&x2B+;真慢 我在Python中开始了一个小程序,但是花了很长时间才完成,所以我切换到C++。我以前没有使用这种特定语言的经验(虽然用c语言编写了很多代码),我是从一个web编辑器开始的: 我的C++代码是: clock_t start, end; /* Recording the starting clock tick.*/ start = clock(); int R = 0; int x = 0; for (R = 6; R <= 10000; R = R + 2) { int X_min = ceil(0.5 * sqrt(2) * R); int N_pairs = 0; for (x = X_min; x < R; x++) { float y = sqrt(pow(R, 2) - pow(x, 2)); if (rint(y) == y) { N_pairs = N_pairs + 1; } } if (N_pairs >= 4) { //cout << R << ", " << N_pairs; //cout << "\n"; } } end = clock(); //Calculating total time taken by the program. double time_taken = double(end - start) / double(CLOCKS_PER_SEC); cout << "Time taken by program is : " << time_taken; cout << " sec " << endl; //cout << "1" << "|" << "2" << "|" << "3 \n"; //cout << "4" << "|" << "5" << "|" << "6 \n"; //cout << "7" << "|" << "8" << "|" << "9 \n"; 时钟开始、结束; /*记录开始的时钟滴答声*/ 开始=时钟(); int R=0; int x=0; 对于(R=6;R=4){ //cout,c++,performance,visual-studio-2017,visual-studio-2019,C++,Performance,Visual Studio 2017,Visual Studio 2019,主要问题是VC++没有内联调用: 通过将rint(y)替换为“手动”舍入,可以获得良好的加速: 改变 if (rint(y) == y) { 到 在我的机器上,从0.8s下降到0.04s(使用/O2/fp:fast编译) 您还需要在循环外使用N_对,否则(好的)编译器可以优化所有内容。优化的第一条规则:不正确程序的性能无关 看起来您正在寻找x^2+y^2=R^2的整数解。但是,使用float数据类型进行中间存储会产生大量误报。将N_对移出循环(如@rustyx所述,防止完全删

主要问题是VC++没有内联调用:

通过将
rint(y)
替换为“手动”舍入,可以获得良好的加速:

改变

        if (rint(y) == y) {

在我的机器上,从0.8s下降到0.04s(使用
/O2/fp:fast
编译)


您还需要在循环外使用
N_对
,否则(好的)编译器可以优化所有内容。

优化的第一条规则:不正确程序的性能无关

看起来您正在寻找
x^2+y^2=R^2
的整数解。但是,使用
float
数据类型进行中间存储会产生大量误报。将
N_对
移出循环(如@rustyx所述,防止完全删除该循环,并计算所有对)结果:7886对;其中
double
:5681对

最后一个数字也对应于完全整数检查,这要快一点(在我的系统上为21毫秒)。以下是我的代码:

#include <iostream>
#include <chrono>

int main()
{
    auto t = std::chrono::high_resolution_clock::now();
    int N_pairs = 0;
    double d = 0.5 * sqrt(2);
    for (int R = 6; R <= 10000; R = R + 2) {
        int X_min = ceil(d * R);

        for (int x = X_min; x < R; x++) {
            int y = sqrtf(R * R - x * x);
            if(x*x + y*y == R*R) {
                N_pairs = N_pairs + 1;
            }
        }
    }
    std::cout << "Time taken by program is: " 
        << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - t).count() 
        << " ms" << std::endl;
    std::cout << "N_pairs: " << N_pairs << std::endl; // with float: 7886; with double or int: 5681
return 0;
}
#包括
#包括
int main()
{
自动t=std::chrono::高分辨率时钟::现在();
int N_对=0;
双d=0.5*sqrt(2);

对于(int R=6;R)您是否启用了发布版本配置?正如@walnut所说。使用不同的优化/构建标志运行可能会起到很大的作用。请参阅:其中一个使用优化级别3,另一个使用0。对我来说,这是时间差的30倍。(尽管编译器与msvc不同)我确实启用了发布版本配置。如果没有它,则需要更长的时间:8.701秒您无法在Windows上使用
clock
来比较性能。使用
sqrt(R*R-x*x)
来避免pow(),确保回复的目标是x64.thx。我之所以在循环中有N_对,是因为我对每个'R'的对数感兴趣,而不是对所有R'的对总数感兴趣。也许我应该每次都将其设置为零,而不是反复声明,但我还没有检查。这是我第一次必须优化一个程序。另外,你声明变量y为整数的事实不会引入误报吗?因为你马上对sqrt进行了四舍五入。我尝试将它改为双倍,虽然它对R高达10.000的结果没有影响,但对R高达100的结果却有影响。000@user11454816-我的代码不能有误报,因为要进行整数测试,以查看和平方的平方等于R平方。由于您提到的舍入,它可能有误报;我还将通过检查
y+1
-平方是否有效来解决这个问题。
        if (int(y+0.5) == y) {
#include <iostream>
#include <chrono>

int main()
{
    auto t = std::chrono::high_resolution_clock::now();
    int N_pairs = 0;
    double d = 0.5 * sqrt(2);
    for (int R = 6; R <= 10000; R = R + 2) {
        int X_min = ceil(d * R);

        for (int x = X_min; x < R; x++) {
            int y = sqrtf(R * R - x * x);
            if(x*x + y*y == R*R) {
                N_pairs = N_pairs + 1;
            }
        }
    }
    std::cout << "Time taken by program is: " 
        << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - t).count() 
        << " ms" << std::endl;
    std::cout << "N_pairs: " << N_pairs << std::endl; // with float: 7886; with double or int: 5681
return 0;
}