linux环境下 C++性能测试工具 gprof + kprof + gprof2dot

摘要:
gprof非常有名。在Googleg++-pg-g-otest.cc./test/gmon.exgprof上有很多教程/test>profLog让我们来看看我前面提到的huffman编码压缩+解码解压缩的整个过程Log,哇,这很有用,但似乎有点累,不是吗:)CodeFlatprofile:Eachsamplecountsas0.01secon

1.gprof

很有名了,google下很多教程

g++ -pg -g  -o test test.cc

./test   //会生成gmon.out

gprof ./test > prof.log

看一下对于我前面提到的huffman编码压缩+解码解压缩全部过程的一个程序

对于生成的prof.log ,wow, 很有用处的但是看起来有点累,不是吗:)

linux环境下 C++性能测试工具 gprof + kprof + gprof2dot第1张linux环境下 C++性能测试工具 gprof + kprof + gprof2dot第2张Code
Flat profile:
Each sample counts as 0.01 seconds.
%   cumulative   self              self     total           
time   seconds   seconds    calls   s/call   s/call  name    
32.51 1.71 1.71 13127166 0.00 0.00  glzip::HuffTree<unsigned char, glzip::decode_hufftree>::decode_byte(unsigned char, glzip::Buffer&, glzip::HuffNode<unsigned char>*&, int)
18.73 2.69 0.98 105017328 0.00 0.00  glzip::Buffer::write_bit(int)
16.63 3.57 0.88 24292128 0.00 0.00  glzip::Buffer::write_string(std::string const&)
7.22 3.95 0.38 61711820 0.00 0.00  glzip::Buffer::read_byte(unsigned char&)
5.80 4.25 0.30 37419691 0.00 0.00  glzip::Buffer::write_byte(unsigned char)
5.51 4.54 0.29 1 0.29 2.48  glzip::Encoder<unsigned char>::do_encode_file(glzip::char_tag)
5.13 4.82 0.27 105017721 0.00 0.00  glzip::HuffNode<unsigned char>::is_leaf()
3.04 4.97 0.16 1 0.16 0.31  glzip::Encoder<unsigned char>::do_caculate_frequency(glzip::char_tag)
2.47 5.11 0.13 1 0.13 2.42  glzip::HuffTree<unsigned char, glzip::decode_hufftree>::decode_file()
1.14 5.17 0.06 24292227 0.00 0.00  std::vector<std::string, std::allocator<std::string> >::operator[](unsigned int)
0.95 5.21 0.05 1 0.05 0.05  glzip::HuffTree<unsigned char, glzip::encode_hufftree>::do_serialize_tree(glzip::HuffNode<unsigned char>*, glzip::Buffer&)
0.29 5.23 0.01 24292133 0.00 0.00  glzip::HuffTreeBase<unsigned char>::root() const
0.29 5.25 0.01 574 0.00 0.00  glzip::Buffer::flush_buf()
0.19 5.25 0.01 6 0.00 0.00  glzip::Buffer::~Buffer()
0.10 5.26 0.01                             unsigned int const& std::min<unsigned int>(unsigned int const&, unsigned int const&)
0.00 5.26 0.00 12901 0.00 0.00  std::_Deque_iterator<glzip::HuffNode<unsigned char>*, glzip::HuffNode<unsigned char>*&, glzip::HuffNode<unsigned char>**>::_Deque_iterator(std::_Deque_iterator<glzip::HuffNode<unsigned char>*, glzip::HuffNode<unsigned char>*&, glzip::HuffNode<unsigned char>**> const&)
0.00 5.26 0.00 6065 0.00 0.00  std::__deque_buf_size(unsigned int)
0.00 5.26 0.00 6056 0.00 0.00  std::_Deque_iterator<glzip::HuffNode<unsigned char>*, glzip::HuffNode<unsigned char>*&, glzip::HuffNode<unsigned char>**>::_S_buffer_size()
0.00 5.26 0.00 5656 0.00 0.00  std::_Deque_iterator<glzip::HuffNode<unsigned char>*, glzip::HuffNode<unsigned char>*&, glzip::HuffNode<unsigned char>**>::operator*() const
0.00 5.26 0.00 5656 0.00 0.00  std::_Deque_iterator<glzip::HuffNode<unsigned char>*, glzip::HuffNode<unsigned char>*&, glzip::HuffNode<unsigned char>**>::operator+=(int)
0.00 5.26 0.00 4871 0.00 0.00  std::_Deque_iterator<glzip::HuffNode<unsigned char>*, glzip::HuffNode<unsigned char>*&, glzip::HuffNode<unsigned char>**>::operator+(int) const
0.00 5.26 0.00 3126 0.00 0.00  glzip::HuffNode<unsigned char>::weight() const
0.00 5.26 0.00 1465 0.00 0.00  glzip::HuffTree<unsigned char, glzip::encode_hufftree>::HuffNodePtrGreater::operator()(glzip::HuffNode<unsigned char> const*, glzip::HuffNode<unsigned char> const*)
0.00 5.26 0.00 946 0.00 0.00  glzip::Buffer::fill_buf()
0.00 5.26 0.00 785 0.00 0.00  std::_Deque_iterator<glzip::HuffNode<unsigned char>*, glzip::HuffNode<unsigned char>*&, glzip::HuffNode<unsigned char>**>::operator-(int) const
0.00 5.26 0.00 785 0.00 0.00  std::_Deque_iterator<glzip::HuffNode<unsigned char>*, glzip::HuffNode<unsigned char>*&, glzip::HuffNode<unsigned char>**>::operator-=(int)
0.00 5.26 0.00 590 0.00 0.00  glzip::HuffNode<unsigned char>::left() const
0.00 5.26 0.00 590 0.00 0.00  glzip::HuffNode<unsigned char>::right() const
0.00 5.26 0.00 453 0.00 0.00 operator new(unsigned int, void*)
linux环境下 C++性能测试工具 gprof + kprof + gprof2dot第3张.

2.Kprof

sudo apt-get install kprof

kprof -f ./test //注意已经按照前面的用gprof生成 gmont.out 了。

将gprof , GUI化了,更加友好,还是很方便的,它也利用而来graphviz绘制了运行时流程图,但是对于大的程序,

似乎效果不太好,流程图感觉完全乱掉了,而且对于大程序,kprof运行的有点慢,在我的破机器上跑半天才出来。

总的来说它的object profile还是很方便有用的。

object profile 示意图

image

graph view 示意图,这个仅作参考吧,同样是用graphviz怎么和gprof2dot差距这么大呢?:)对比下下面gprof2dot的生成图吧!

image

3.gprof2dot.py

这个脚本太TMD的牛X了,速度快效果好,怪不得底下评论一片叫好,这就是life_saving的工具啊!

首先就是运行时的程序流程图,网上有很多工具有些就是利用gprof的,有很多文章介绍,都不用看了,就用这个吧。

然后很直观的看出每个步骤的占用时间百分比,函数调用次数,颜色能直观的表示出瓶颈所在。

gprof2dot安装看这里吧

http://www.51testing.com/?uid-13997-action-viewspace-itemid-79952

gprof ./test | gprof2dot.py | xdot

恩 去下载那个xdot.py吧 看dot文件,太方便了,不需要先生成图像了。

直接看效果图吧!

1.整体图

   清晰多了吧,上面那么多冗余的东西。

image

2.局部图

image

 image

和我的程序执行过程完全对应,清晰明了,太赞了!

每个方框内部显示函数名称,函数整体包括内部子函数占用时间%比,函数自身,不包括内部子函数占用时间%比,函数执行次数。

边表示父函数调用该子函数占用的时间%比,调用次数。

 之所以清晰是因为,gprof2dot默认是部分函数调用图,对性能影响不大的函数调用都不显示,例如上图中没有出现类的构造,析构函数,

 如果想要显示全部的函数调用,可以 gprof2dot -n0 -e0 ,默认是n0.5即影响小于5%的函数就不显示了。

当然这样图片会很乱,因为显示内容很多,可以 gprof2dot -n0 -e0 -s #-s表示不显示诸如模板,函数入口参数等等,使得

函数名称显示更加精简。

恩,如果用户能决定只输出某个函数及其关联函数的显示信息图就更好了,例如上面有两个

独立的过程compress()和decompress()如果用户能够决定只是显示compress()就好了,

不过考虑到模板之类的稍微有点麻烦,但是应该也是能实现的!

4. google 还有一个开源的能测试工具

以后尝试一下

http://code.google.com/p/google-perftools/wiki/GooglePerformanceTools

 

免责声明:文章转载自《linux环境下 C++性能测试工具 gprof + kprof + gprof2dot》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇matlab演奏《卡农》DES加密/解密类。下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

解决linux重启后无法开启nginx问题“var/run/nginx/nginx.pid" no such file or directory问题

起因:每次重启虚拟机后,var/run/nginx/目录都会被删除,所以无法在这个目录创建nginx.pid文件,   可以自己创建var/run/nginx/目录,然后可以运行,但治标不治本,下次虚拟机重启目录还会丢失。 解决方法:   打开/usr/local/nginx/conf/nginx.conf 这是我的配置文件,一般在nginx的conf文件...

picker-view、微信小程序自定义时间选择器(非官方)

picker-view自定义时间选择器 官网的自定义时间选择器比较简陋、日期不准 下面是我自己写的一个demo <view class="baseList"> <view class="list clearfix"> <view class="fl listName"><text class="req...

cmake 备忘录

记录使用 cmake 时的常见需求和解决办法。 目录 1. 用于执行CMake的.bat脚本 2. 判断平台:32位还是64位? 3. 判断Visual Studio版本 4. 判断操作系统 5. 判断是Debug还是Release等版本 6. 根据Debug/Release添加不同的库目录 7. Visual Studio属性与对应CMake实现方法...

mmap操作荔枝派gpio v3s Linux

1.预备知识 https://baike.baidu.com/item/mmap/1322217?fr=aladdin 大家直接看百度百科,比较清楚了。 2.代码 gpio.c #include "gpio.h" unsigned int fd; PIO_Map *PIO = NULL; unsigned int *gpio_map; unsigned...

mount(挂载)

拷贝文件到优盘 sdcm@sdcm:/mnt$sudo fdisk -l Disk /dev/sdc: 15.5 GB, 15529279488 bytes255 heads, 63 sectors/track, 1887 cylinders, total 30330624 sectorsUnits = sectors of 1 * 512 = 512 b...

Linux下使用VirtualBox安装Windows系统

(文档比较长,只是写的详细,实际操作起来相对简单。)由于一些特殊原因,我们并不能完全抛下Windows而使用Linux。VirtualBox 是一款虚拟机软件,支持多系统。在Linux下安装 VirtualBox 后,我们就可以安装Windows系统,并使用Windows下的软件了。1、下载首先需要下载 VirtualBox,下载地址:http://www...