对于下面的这段代码:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
int 
main()
{
    const int n = 1e9;
    for(int i = 0; i < n; ++i)
    {
        //~ nothing
    }
    return 0;
}

正常的编译命令:

1
$ g++ test.cpp -o test

用time测试运行时间:

1
time ./test

输出:

real	0m4.431s
user	0m4.068s
sys	0m0.012s

使用优化选项编译:

1
$ g++ test.cpp -o test -O2

此时的运行时间:

real	0m0.008s
user	0m0.000s
sys	0m0.004s

so amazing!
g++有四个级别的优化选项,分别对应于 -O1, -O2, -O3, -O4.

Tags: ,.
你好!除了代码,此处没有多少原创之物,皆为本人搜集、整理、总结之记录与心得,欢迎转载分享!转载时请尽量注明出处,将不胜感激。祝你健康、快乐!
Home

Be the first to comment on this entry.

Name(required)
Mail (required),(will not be published)

RFC: Request For Comments. Orz..

Website(recommended)