linux 程序性能分析工具 gprof

 

linux 程序性能分析工具 gprof

https://www.imooc.com/article/273770?block_id=tuijian_wz 

 

使用方式

  • 编译时加-pg选项,程序正常退出时,生成gmon.out,通过gprof可以查看gmon.out中的统计结果。

  • gprof -b exe_file 生成分析日志,-b减少冗余的说明信息

 

#define MAX 10000000void f() {    long long sum = 0;    for (long long i=0;i<MAX;i++)
        sum += i;
}void g() {    long long sum = 0;    for (long long i=0;i<MAX;i++)
        sum += i;
    f();
}int main() {    long long sum = 0;    for (long long i=0;i<MAX;i++)
        sum += i;
    f();
    g();
}// 编译:g++ -p -o demo demo.cpp// 运行:./demo// 统计:gprof -b demo

 

 

  

Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls  ms/call  ms/call  name    
 36.12      0.05     0.05        2    25.28    25.28  f() 36.12      0.10     0.05                             main 28.89      0.14     0.04        1    40.45    65.73  g()///////////////////////////////////////////////////////////////// %time              : 各个函数占用的时间比例,加和应该为 100% 
cumulative seconds : 累计时间,当前行函数耗时为当前行累计时间减去上一行self seconds       : 当前函数耗时不包含子函数
calls              : 调用次数self ms/call       : 调用一次平均时间(不包含子函数)
total ms/call      : 调用一次平均耗时(包括子函数)
name               : 函数名////////////////////////////////////////////////////////////////
            Call graph///////////////////////////////////////////////////////////////// 1, 每个函数对应1个 index, 每个函数对应一个 entry2, 不同 entry 之间虚线分割3,每个 entry 中以 index 开始的行成为 primary line4,primary 上面的为 caller line, 下面的为 subroutine line////////////////////////////////////////////////////////////////granularity: each sample hit covers 2 byte(s) for 7.06% of 0.14 seconds

index % time    self  children    called     name
                                                 <spontaneous>
[1]    100.0    0.05    0.09                 main [1]                0.04    0.03       1/1           g() [2]                0.03    0.00       1/2           f() [3]
-----------------------------------------------                0.04    0.03       1/1           main [1]
[2]     46.4    0.04    0.03       1         g() [2]///////////////////////////////////////////////////////////////// 1, primary line2, index - 23, time 耗时占比为 46.44, self 当前函数耗时 0.04 不包含子函数5, children 子函数耗时 0.036, called 调用次数////////////////////////////////////////////////////////////////
                0.03    0.00       1/2           f() [3]///////////////////////////////////////////////////////////////// 1, subroutine line2, self 当前函数耗时 0.03 不包含子函数3, children 子函数耗时 0.004, called 1/2 f()共被调用两次,g调用1次////////////////////////////////////////////////////////////////-----------------------------------------------                0.03    0.00       1/2           g() [2]                0.03    0.00       1/2           main [1]
[3]     35.7    0.05    0.00       2         f() [3]
-----------------------------------------------

Index by function name

   [3] f()                     [2] g()                     [1] main


 

posted @ 2021-05-15 11:38  博客园—哆啦A梦  阅读(361)  评论(0)    收藏  举报