gperftools分析cpu使用情况

来源:互联网 发布:吉林11选5遗漏数据 编辑:程序博客网 时间:2024/05/18 15:08

gperftools是Google提供的一套工具,其中的一个功能是CPU profiler,用于分析函数的执行时间,安装见参考链接,下面是一个简单的使用实例;

一 .代码
test_profiler.cpp

  1 #include<iostream>  2 #include<google/profiler.h>  3 using namespace std;  4   5 void testfunc()  6 {  7     int sum = 0;  8     for(int i =0; i < 700000000; ++i)  9     { 10         sum +=i; 11     } 12     std::cout<<"sum:"<<sum<<endl; 13  14 } 15 int main() 16 { 17     ProfilerStart("first.prof"); 18     std::cout<<"hello profiler"<<endl; 19     testfunc(); 20  21     int s = 0; 22  23     for(int i = 0; i < 300000000; ++i) 24     { 25         s +=i; 26     } 27  28     std::cout<<"int main s:"<<s<<endl; 29  30     ProfilerStop(); 31     return 0; 32 }~                    

二 .编译
Makefile

all:test_profiler.cpp  2     g++ -o test_profile test_profile.cpp -lprofiler -lunwind

三.运行程序

./test_profiler hello profilersum:-1659114368int main s:-302797184PROFILE: interrupts/evictions/bytes = 255/23/1440

三.查看分析结果

pprof –text ./test_profiler test_profiler.prof

Using local file ./test_profiler.Using local file test_profiler.prof.Removing killpg from all stack traces.Total: 255 samples     179  70.2%  70.2%      179  70.2% testfunc      76  29.8% 100.0%      255 100.0% main       0   0.0% 100.0%      255 100.0% __libc_start_main

也可以以图片方式查看
pprof –pdf ./test_profiler test_profiler.prof > output.pdf

参考:

关于gperftools
http://www.cnblogs.com/caosiyang/archive/2013/01/25/2876244.html

google perftools分析程序性能
http://www.cnblogs.com/caosiyang/archive/2013/01/25/2876244.html

安装perftools
http://www.2cto.com/os/201209/155686.html

0 0
原创粉丝点击