《Linux环境下C编程指南(第二版)》p63页介绍了其他的编译调试工具,其中提到了gprof。
参考链接:
http://www.cnblogs.com/feisky/archive/2010/03/09/1681997.html
在编译程序时加上-pg选项,就可以在程序中使用pgrof,它在程序每次执行时产生一个叫gmon.out文件,gprof就使用这个文件来剖析信息。
程序a.exe执行之后生成gmon.out,其使用方法如下图:
# gprof.exe -b a.exe gmon.out
其中相关字段的简单描述:
time -- 函数使用时间占所有时间的百分比。
self -- 函数本身所执行的时间。
可以根据time工具的信息和上图进行对照,time执行的结果:
附上示例代码:
#include <stdio.h>
int main()
{
int counter;
int ending;
int temp;
int five;
for (counter = 0; counter < 2 * 100000000 * 9 / 18 + 5131; counter += (5-3)/2) {
temp = counter / 15302;
ending = counter;
five = 5;
}
printf("five = %d; ending = %d\n", five, ending);
return 0;
}