有个关于c++编译和运行速度的问题!!!!!
Tofloor
poster avatar
ritter
deepin
2017-12-07 15:09
Author
今日闲来无事,写了个找素数的程序,我在deepin下用g++编译后,找100000个素数用了16秒, 但是我在windows下用mingw的g++编译后,同样找100000个数用了47秒,这是什么原因?两个命令行都是 g++ test.cpp -o test -O2

我之后用visual studio的release模式便宜了一下,跑的比乌龟还慢。。。。。。

哪位大神能指点一下?完全搞不懂。。。。。

##################代码部分########################################
#include
#include
#include

using namespace std::chrono;
using namespace std;

int get_prime(vector& vec, int num){
    for(auto i : vec){
        if (num%i==0){
            return 0;
        }
    }
    return num;
}

void prime(int n){
    vector vec;
    cout << "prime number found -> 2\n";
    int k = 2;
    int num = 3;
    while(k < n){
        auto result = get_prime(vec, num);
        if(result != 0){
            cout << "prime number found -> " << result << "\n";
            vec.push_back(result);
            ++k;
        }
        ++num;
    }
}


int main(int argc, char** argv){
    auto t1 = high_resolution_clock::now();
    prime(atoi(argv[1]));
    auto t2 = high_resolution_clock::now();
    cout <<"It costs "<< duration_cast(t2-t1).count() <<" milliseconds" << endl;
    return 0;
}


Reply Favorite View the author
All Replies
2 / 2
To page
avatar
ritter
deepin
2017-12-09 10:06
#21
https://bbs.deepin.org/post/149735
试试用clang编译啊

跟g++速度差不多,大小也差不多。
Reply View the author
avatar
ritter
deepin
2017-12-09 10:08
#22
https://bbs.deepin.org/post/149735
输出汇编指令对比一下

看不懂汇编。。。。。
Reply View the author
2 / 2
To page