[英]Same code has vastly different run times on different machines
If I run this code 如果我运行此代码
#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);
}
on this web site: http://cpp.sh/ the running time is around 0.16 seconds however if I compile and run this on my home machine (i5 4460 16 gb ram, mechanical HDD,visual studio 2017) the running time is around 6.6 seconds which is around 41 times slower, what is causing such a huge speed difference? 在这个网站上: http : //cpp.sh/运行时间约为0.16秒,但如果我在家用机器上编译并运行它(i5 4460 16 gb ram,机械硬盘,视觉工作室2017),运行时间约为6.6秒这大约慢了41倍,是什么导致如此巨大的速度差异?
thanks 谢谢
My psychic powers suggest that you built a Debug build on your home machine with Visual Studio. 我的通灵能力建议你使用Visual Studio在家用机器上构建一个Debug版本。 Flip the Solution to a Release build and watch the performance go way up. 将解决方案翻转到发布版本并观察性能上升。
I just tested this with Visual Studio. 我刚用Visual Studio测试过。 The difference between Debug and Release is 100x with your code. Debug和Release之间的差异是你的代码的100倍。 (8 seconds vs .08 seconds). (8秒vs。08秒)。
It's not that Debug builds are inherently slow. 并不是Debug构建本质上很慢。 It's just that really tight loops doing math, memory, or anything non-blocking can be highly optimized. 这只是非常紧凑的循环,可以高度优化数学,内存或任何非阻塞。
different mechanisms to deal with memory management perhaps? 或许处理内存管理的不同机制? it's hard to pinpoint exactly as there can be quite a lot of assumptions, but there are ways to make your code faster on both ends. 由于可能存在相当多的假设,因此很难确切地指出,但是有一些方法可以使代码在两端都更快。
#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;
no need to clear something that's empty. 无需清除空洞的东西。
int entry_size = 4;
int population_size = 500000;
vector<string> population;
pre-alloc the vector, 500000 is a huge number and the vector would have to do many allocations. 预先分配向量,500000是一个巨大的数字,向量必须做很多分配。
population.resever(population_size);
also, pre-alloc the string random, and don't clear it every loop. 另外,预先分配字符串随机,并且不要在每个循环中清除它。 the amount of memory is small but allocating and unallocating is expensive. 内存量很小,但分配和取消分配是很昂贵的。 instead change the values of the preallocated string dinamically. 而是以dinamically方式更改预分配字符串的值。
random.reserve(entry_size);
for (int i = 0; i < population_size; i++) {
you used 'i' as the keys for the inner and the outer for loops, this looks like a bug, I'm changing the index of the inner loop to j. 你使用'i'作为内部和外部for循环的键,这看起来像一个bug,我正在将内循环的索引更改为j。
for (int j = 0; j < (unsigned)entry_size; j++) {
randChar = rand() % 25 + 97;
random[j] = randChar;
}
population.push_back(random);
}
int stop_s = clock();
cout<< (stop_s - start_s)/(double)(CLOCKS_PER_SEC);
}
This should make your app quite faster. 这应该会让你的应用程序更快。
我认为这个因素中的一些会产生影响:CPU,操作系统,内存大小,I / O速度(不是因为你的代码没有任何IO),内存缓存,并行化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.