GProf的使用

GProf的使用

  • 1、安装gprof2dot.py

    下载地址 http://jrfonseca.googlecode.com/svn/trunk/gprof2dot/gprof2dot.py
    得到 gprof2dot-2021.2.21.tar.gz
    解压 tar -zvxf gprof2dot-2021.2.21.tar.gz
    
  • 2、配置gprof2dot.py环境变量

    • 1、当前终端单次有效 执行命令 export PATH=$PATH:/home/gzrobot/gprof_dir/gprof2dot-2021.2.21

    • 2、当前用户永久有效

      vi ~/.profile

      通过 “:” 分割追加PATH

在这里插入图片描述

  • 3、配置所有用户有效

    vi /etc/profile

    在最后加入 export PATH="=$PATH:/home/gzrobot/gprof_dir/gprof2dot-2021.2.21"

    再使用 source /etc/profile 使配置生效

#include <iostream>
#include <thread>
using namespace std;

int add(int a, int b)
{
        for(int i = 0; i < 10000; ++i)
        {
                ;
        }
        return a + b;
}

int sub(int a, int b)
{
        for(int i = 0; i < 5000; ++i)
        {;}
        return a - b;
}

int call()
{
        cout << add(1, 2) << endl;
        cout << sub(2, 4) << endl;
}


int main()
{
        for(int i = 0; i < 10000; ++i)
                call();
        return 0;
}


  • 编译

    g++ -o test_gprof test_gprof.cpp -pg -std=c++11
    
  • 运行程序,生成gmon.out

  • 查看输出

    gprof ./prog gmon.out -b
    
     gzrobot@gzrobot:~/Gprof$ gprof ./test_gprof gmon.out -b
    Flat profile:
    
    Each sample counts as 0.01 seconds.
      %   cumulative   self              self     total
     time   seconds   seconds    calls  us/call  us/call  name
     57.13      0.30     0.30    10000    30.28    30.28  add(int, int)
     43.80      0.53     0.23    10000    23.21    23.21  sub(int, int)
      0.00      0.53     0.00    10000     0.00    53.50  call()
      0.00      0.53     0.00        1     0.00     0.00  _GLOBAL__sub_I__Z3addii
      0.00      0.53     0.00        1     0.00     0.00  __static_initialization_and_destruction_0(int, int)
    
                            Call graph
    
    
    
    granularity: each sample hit covers 2 byte(s) for 1.87% of 0.53 seconds
    
    index % time    self  children    called     name
                                                     <spontaneous>
    [1]    100.0    0.00    0.53                 main [1]
    
                    0.00    0.53   10000/10000       call() [2]
    -----------------------------------------------
    
                    0.00    0.53   10000/10000       main [1]
    
    [2]    100.0    0.00    0.53   10000         call() [2]
                    0.30    0.00   10000/10000       add(int, int) [3]
    
                    0.23    0.00   10000/10000       sub(int, int) [4]
    -----------------------------------------------
    
                    0.30    0.00   10000/10000       call() [2]
    
    [3]     56.6    0.30    0.00   10000         add(int, int) [3]
    -----------------------------------------------
    
                    0.23    0.00   10000/10000       call() [2]
    
    [4]     43.4    0.23    0.00   10000         sub(int, int) [4]
    -----------------------------------------------
    
                    0.00    0.00       1/1           __libc_csu_init [18]
    
    [11]     0.0    0.00    0.00       1         _GLOBAL__sub_I__Z3addii [11]
    
                    0.00    0.00       1/1           __static_initialization_and_destruction_0(int, int) [12]
    -----------------------------------------------
    
                    0.00    0.00       1/1           _GLOBAL__sub_I__Z3addii [11]
    
    [12]     0.0    0.00    0.00       1         __static_initialization_and_destruction_0(int, int) [12]
    -----------------------------------------------
    
    Index by function name
    
      [11] _GLOBAL__sub_I__Z3addii [4] sub(int, int)           [2] call()
       [3] add(int, int)          [12] __static_initialization_and_destruction_0(int, int)
                                                            , int)
    
    
    
  • 生成图表

    gprof ./tmp | gprof2dot.py |dot -Tpng -o report.png

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值