您的位置:首页 > 编程语言

Gcc中使用profile工具优化代码

2010-10-13 11:29 417 查看
Gcc
中使用
profile
工具优化代码

作者:
wulong710

目的,查询代码中各个函数被调用了几次、在何处被调用。方便程序员对代码进行优化。

环境为
mingw32

编辑如下代码
:

#include "iostream"

#include "stdlib.h"

using namespace std;

int func_ten ()

{

_sleep
(500);

cout
<< "exect func ten" <<endl;

return
0;

}

int func_once ()

{

_sleep
(300);

cout
<< "exect func once" <<endl;

func_ten
();

return
0;

}

int main(void)

{

for
(int i = 0; i < 10; i++)

func_ten
();

func_once
();

return
0;

}


profile
模式编译

$ g++ -Wall -pg -o he test.cpp

执行
he.exe
生成
gmon.out
日志

$ ./he.exe

执行
gprof
将日志写入
log.txt

$ gprof he.exe > log.txt

查看
log.txt

$ vim log.txt

结果显示:

func_ten
被调用了
11
次,其中
10
次为
main
所调,一次为
func_once
所调。

func_once

main
函数调用一次


%
cumulative
self
self
total
^M

time

seconds
seconds
calls

Ts/call
Ts/call
name

^M

0.00
0.00
0.00

11
0.00

0.00
func_ten()
^M

0.00
0.00
0.00

1
0.00

0.00
func_once()
^M

^

index % time
self

children
called
name^M

0.00
0.00

1/11
func_once() [7]^M

0.00
0.00

10/11
_main [1240]^M

[4]

0.0
0.00
0.00

11
func_ten() [4]^M

-----------------------------------------------^M

0.00
0.00

1/1
_main [1240]^M

[7]

0.0
0.00
0.00

1
func_once() [7]^M

0.00
0.00

1/11
func_ten() [4]^M

-----------------------------------------------^M

结束
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: