简介
GNU Profiler (gprof) 是Linux 自带的性能测试工具,可以生成 C, Pascal, Fortran77 程序的函数级别的耗时报表
使用
以下面的 pgtest.cpp 为例说明
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int a(void) {
int i=0,g=0;
while(i++<100000)
{
g+=i;
}
return g;
}
int b(void) {
int i=0,g=0;
while(i++<400000)
{
g+=i;
}
return g;
}
int main(int argc, char** argv)
{
int iterations;
if(argc != 2)
{
printf("Usage %s <No of Iterations>\n", argv[0]);
exit(-1);
}
else
iterations = atoi(argv[1]);
printf("No of iterations = %d\n", iterations);
while(iterations--)
{
a();
b();
}
}
- 编译上面的文件:g++ pgtest.cpp -g -o pgtest -pg -lc
- 然后运行 ./pgtest 100
- 运行过后,会生成 gmon.out(下面通过这个二进制中间文件进而生成report)
- 通过 gprof pgtest gmon.out -p > flat-report 生成 Flat profile report;或者通过 gprof pgtest gmon.out -q > callgraph-report 生成 Call graph profile report
Flat profile report:
Flat profile:
Each sample counts as 0.01 seconds.
% cumulative self