gpt4 book ai didi

c++ - 相同的代码在不同的机器上运行时间大不相同

转载 作者:太空狗 更新时间:2023-10-29 23:49:15 26 4
gpt4 key购买 nike

如果我运行这段代码

#include <iostream>
#include <string>
#include <stdlib.h>
#include <vector>
#include <time.h>
#include <thread>
#include <ctime>
using namespace std;
int main()
{
int start_s = clock();
char randChar;
string random;
random.clear();
int entry_size = 4;
int population_size = 500000;
vector<string> population;
for (int i = 0; i < population_size; i++) {
for (int i = 0; i < (unsigned)entry_size; i++) {
randChar = rand() % 25 + 97;
random += randChar;
}
//cout << random<<endl;
population.push_back(random);
random.clear();
}
int stop_s = clock();
cout<< (stop_s - start_s)/(double)(CLOCKS_PER_SEC);
}

在此网站上:http://cpp.sh/运行时间约为0.16秒但是,如果我在我的家用机器(i5 4460 16 gb ram,机械硬盘,visual studio 2017)上编译并运行它,运行时间约为 6.6 秒,慢了大约 41 倍,是什么导致了如此巨大的速度差异?
谢谢

最佳答案

我的灵力建议您使用 Visual Studio 在家用计算机上构建了一个Debug 构建。将解决方案转换为发布构建并观察性能的提升。

我刚刚使用 Visual Studio 对此进行了测试。调试和发布之间的差异是您的代码的 100 倍。 (8 秒对 0.08 秒)。

并不是说调试构建天生就慢。只是可以高度优化执行数学、内存或任何非阻塞的非常紧密的循环。

关于c++ - 相同的代码在不同的机器上运行时间大不相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43999739/

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