有个关于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
1 / 2
To page
avatar
jhkwei
deepin
2017-12-07 17:57
#1
这程序除了vector 不知道干了什么,其它都不影响,还有就是mingw 是32 位, deepin 是 64 位,建议自己写一个 vector 之类性线表或链表。
Reply View the author
avatar
MeowSprite
deepin
2017-12-07 18:56
#2
g++版本一样吗?
还有关键是vector在加入数据的时候会不断地申请新内存,而申请内存的机制在mingw和linux下是不同的。你可以试试将vector初始化的时候就设为n大小。
还有上面说的32位和64位的原因也会有影响。
Reply View the author
avatar
DebuggerX
deepin
2017-12-07 20:07
#3
想起来好像是哪个知乎大神说的一句话‘一切不看优化选项不看汇编不看profile分析的性能比较都是耍流氓’
Reply View the author
avatar
s***7@gmail.com
deepin
2017-12-08 00:47
#4
你要是用visual studio 的debug模式,那可能比海参还慢
Reply View the author
avatar
ritter
deepin
2017-12-08 01:16
#5
https://bbs.deepin.org/post/149735
这程序除了vector 不知道干了什么,其它都不影响,还有就是mingw 是32 位, deepin 是 64 位,建议自己写一 ...

那看了写c++还得在linux,不仅编译快,跑得还快一点。在windows下编译都慢很多,估计link的机制都不一样。
Reply View the author
avatar
ritter
deepin
2017-12-08 01:20
#6
https://bbs.deepin.org/post/149735
g++版本一样吗?
还有关键是vector在加入数据的时候会不断地申请新内存,而申请内存的机制在mingw和linux下 ...

g++ (x86_64-posix-seh-rev1, Built by MinGW-W64 project) 7.2.0
deepin下就是用apt-get install build-essential,版本应该就是deepin提供的最新版本。
x86_64不应该也是64位版本吗?
Reply View the author
avatar
ritter
deepin
2017-12-08 01:20
#7
https://bbs.deepin.org/post/149735
想起来好像是哪个知乎大神说的一句话‘一切不看优化选项不看汇编不看profile分析的性能比较都是耍流氓’[jo ...

优化选项就是-O2啊,我上面写了。
Reply View the author
avatar
ritter
deepin
2017-12-08 01:21
#8
https://bbs.deepin.org/post/149735
你要是用visual studio 的debug模式,那可能比海参还慢

我测试的时候就是用debug模式,跑了2分30秒。。。。。。
Reply View the author
avatar
duanyao
deepin
2017-12-08 01:22
#9
你不应该在追求性能的代码里插入 IO(包括stdio)操作,也就是 cout << "prime number found -> " << result << "\n"
我去掉这句后在 deepin 上提高了 30% 的速度,Windows 没试过。
stdio 的速度应该还与 shell 和终端的实现有关,不是只跟你自己的代码有关,Windows 和 cygwin 与 Linux 应该有较大的差异。
另外,不同编译器的 C++ 标准库实现(包括 vector)也是不一样的哦。
Reply View the author
avatar
ritter
deepin
2017-12-08 01:29
#10
https://bbs.deepin.org/post/149735
你不应该在追求性能的代码里插入 IO(包括stdio)操作,也就是 cout

找到了输出才有意义嘛,不输出那找到了也看不到。不过windows下写c或者c++真的很难受,就是感觉慢,linux要是对gpu的支持跟windows一样好的话,那就舒服了。
Reply View the author
avatar
duanyao
deepin
2017-12-08 01:42
#11
https://bbs.deepin.org/post/149735
找到了输出才有意义嘛,不输出那找到了也看不到。不过windows下写c或者c++真的很难受,就是感觉慢,linux ...

输出可以放在计时结束以后呀。

另外我在 deepin 上找 100000 用了 39 秒(去掉输出),所以有点怀疑你的 CPU 是不是真的有那么快,我的是 Core i5 2450M 2.5GHz 。VC++ 的性能不至于有很大差异的,你可以用命令行编译一下,/O2 大致对应 gcc 的 -O2
http://blog.csdn.net/windtalkersm/article/details/5620551
Reply View the author
avatar
ritter
deepin
2017-12-08 02:03
#12
https://bbs.deepin.org/post/149735
输出可以放在计时结束以后呀。

另外我在 deepin 上找 100000 用了 39 秒(去掉输出),所以有点怀疑你的 ...

我的cpu是core i7-4702HQ 2.2GHz,应该是i7里面最烂的了。现在i9都出了,你i5可以换了
Reply View the author
avatar
duanyao
deepin
2017-12-08 03:20
#13
https://bbs.deepin.org/post/149735
我的cpu是core i7-4702HQ 2.2GHz,应该是i7里面最烂的了。现在i9都出了,你i5可以换了 ...

你这个U最大频率 3.2,应该不至于比我的快那么多吧,你再确认一下 100000 没写错?
电脑是公司配的,不坏也没办法。
Reply View the author
avatar
ritter
deepin
2017-12-08 03:28
#14
https://bbs.deepin.org/post/149735
你这个U最大频率 3.2,应该不至于比我的快那么多吧,你再确认一下 100000 没写错?
电脑是公司配的,不坏 ...

是的,100000个(没有去掉输出),最后一个数大概是130-0000左右。
Reply View the author
avatar
yuri
deepin
2017-12-08 19:47
#15
试试用clang编译啊
Reply View the author
avatar
comzhong
deepin
2017-12-08 20:10
#16
输出汇编指令对比一下
Reply View the author
avatar
duanyao
deepin
2017-12-08 20:19
#17
https://bbs.deepin.org/post/149735
是的,100000个(没有去掉输出),最后一个数大概是130-0000左右。

好奇,能把你编译出来的程序发一下吗?这个是我的 getprime.zip

Reply View the author
avatar
ritter
deepin
2017-12-09 09:31
#18
行啊!这是我用g++ -O2编译出来的。
Reply View the author
avatar
ritter
deepin
2017-12-09 09:34
#19
https://bbs.deepin.org/post/149735
好奇,能把你编译出来的程序发一下吗?这个是我的

你的确实慢,我用了27秒。。。。。这是我的
Reply View the author
avatar
ritter
deepin
2017-12-09 09:38
#20
https://bbs.deepin.org/post/149735
好奇,能把你编译出来的程序发一下吗?这个是我的

前面的代码有误,我没有把2放进vector里面,这就导致了很多不是素数的数被打印了出来,所以慢了。
一下是正确的代码:
//##################代码部分##########################
#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;
    int k = 0;
    int num = 2;
    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){
    if(argc != 2) {
        cerr << "too less or too many arguments!" << endl;
        return -1;
    }
   
    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 View the author
1 / 2
To page