1、安装
编译参数增加 -lprofiler -L$(LIB_PROFILER_DIR)
您可以在 google-perftools 的网站 (http://code.google.com/p/google-perftools/downloads/list) 上下载最新版的安装包
2、编译与运行
void bar()
{
for(int i = 0; i < 1000; i++)
{
;
}
}
void foo()
{
for(int i = 0; i < 100000; i++)
{
bar();
}
}
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <gperftools/profiler.h>
#include "DebugData.h"
int main(int argc, char * argv[])
{
char * ptrChar = NULL;
DEBUG_PRINTF( "111111111111........");
ProfilerStart("myprof.txt");
bar();
foo();
ProfilerStop();
}
编译参数增加 -lprofiler -L$(LIB_PROFILER_DIR)
3、结果分析
执行 ./t 会生成 ./myprof.txt
分析结果:
pprof --tools=/usr/bin/ --pdf ./t myprof.txt > myprof.pdf
其中 --tools=/usr/bin/ 非常重要; 不加的话有可能不显示函数名,而仅仅显示地址(为了这问题 折腾了我好几天,好不容易查到资料把问题搞定)