简体   繁体   English

相同的代码在不同的机器上具有截然不同的运行时间

[英]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.

相关问题 C ++和OpenMP-线程的执行时间大不相同 - C++ and OpenMP--vastly different execution times for threads "为什么相同的代码在不同的机器上产生两个不同的 fp 结果?" - Why this same code produce two different fp results on different Machines? 在不同机器上运行时执行速度的差异 - Difference in execution speeds when run on different machines 在不同机器上使用相同端口的错误 - Errors using same port on different machines 在不同机器上执行相同API的时差 - Time difference in executing same API in different machines 在 C++ 中为 2 个字符串实现公共字符计数,但在 2 个字符串上的相同 for 循环中接收到截然不同的字符计数 - Implementing common char count in C++ for 2 strings but receiving vastly different char count for the same for loop on 2 strings 为什么此代码在不同的计算机上导致不同的行为? - Why does this code result in different behavior on different machines? 同一代码的不同版本 - Different different version of same code 为具有对象或对象数组作为成员的类运行不同的代码 - Run different code for a class that has either an object or an array of objects as a member 不同机器上清单中的不同版本 - Different versions in manifest on different machines
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM