gprof简析

gprof是一个用来统计可执行程序数据的工具.它能帮助用户确定程序在哪个地方耗时过多.

被编译器生成指令后的代码将会被gprof捕获并进行计算,gcc编译过程中使用-pg选项编译就会产生指令化工具,当执行编译后的二进制程序时,就会生成一个profile信息的概要文件.

gprof就是用这个文件对程序进行分析的,没有用-pg生成指令化工具的代码是不可测量的.


下面是一个演示程序,如下:


#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <math.h>


double slow(double x)

{

        return pow(x,1.12345);//计算xy次幂

}


double slower(double x)

{

        return 1.0/x;

}


double slowest(double x)

{

        return sqrt(x);//平方根

}



int main (int argc, char *argv[])

{

        int i;

        double x;


        for (i = 0;i < 3000000; i++){

                x = 100.0;

                x = slow(x);

                x = slower(x);

                x = slowest(x);

        }

}

 

我们要用-pg选项对它进行编译,同时为达到最好的效率,我们加上-O2.


编译:

gcc -lm -O2 -pg test.c -o test

 

执行test程序,生成gmon.out文件

查一下gmon.out的文件类型:

file gmon.out 

 

gmon.out: GNU prof performance data - version 1

执行gprof对程序进行分析:

gprof ./test

 

Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total          
 time   seconds   seconds    calls  ns/call  ns/call  name   
 43.48      0.10     0.10  3000000    33.33    33.33  slowest
 21.74      0.15     0.05                             main
 17.39      0.19     0.04  3000000    13.33    13.33  slow
 17.39      0.23     0.04  3000000    13.33    13.33  slower
%         the percentage of the total running time of the
time       program used by this function.

cumulative a running sum of the number of seconds accounted
 seconds   for by this function and those listed above it.

 self      the number of seconds accounted for by this
seconds    function alone.  This is the major sort for this
           listing.

calls      the number of times this function was invoked, if
           this function is profiled, else blank.
 
 self      the average number of milliseconds spent in this
ms/call    function per call, if this function is profiled,
    else blank.

 total     the average number of milliseconds spent in this
ms/call    function and its descendents per call, if this
    function is profiled, else blank.

name       the name of the function.  This is the minor sort
           for this listing. The index shows the location of
    the function in the gprof listing. If the index is
    in parenthesis it shows where it would appear in
    the gprof listing if it were to be printed.


       Call graph (explanation follows)


granularity: each sample hit covers 4 byte(s) for 4.35% of 0.23 seconds

index % time    self  children    called     name
                                                 <spontaneous>
[1]    100.0    0.05    0.18                 main [1]
                0.10    0.00 3000000/3000000     slowest [2]
                0.04    0.00 3000000/3000000     slow [3]
                0.04    0.00 3000000/3000000     slower [4]
-----------------------------------------------
                0.10    0.00 3000000/3000000     main [1]
[2]     43.5    0.10    0.00 3000000         slowest [2]
-----------------------------------------------
                0.04    0.00 3000000/3000000     main [1]
[3]     17.4    0.04    0.00 3000000         slow [3]
-----------------------------------------------
                0.04    0.00 3000000/3000000     main [1]
[4]     17.4    0.04    0.00 3000000         slower [4]
-----------------------------------------------
此后省略

 注:

gprof产生的信息

%                       the percentage of the total running time of the
time                    program used by this function.
                          
函数使用时间占所有时间的百分比。
cumulative          a running sumof the number of seconds accounted
seconds            for by this function and those listed above it.
                          
函数和上列函数累计执行的时间。
self                   the number of seconds accounted for by this
seconds            function alone. This is the major sort for this
                         listing.
                         
函数本身所执行的时间。
calls                  the number of times this function was invoked, if
                         this function is profiled, else blank.
                         
函数被调用的次数
self                  the average number of milliseconds spent in this
ms/call              function per call, if this function is profiled,
                        else blank.
                         
每一次调用花费在函数的时间microseconds
total                 the average number of milliseconds spent in this
ms/call              function and its descendents per call, if this
                         function is profiled, else blank.
                         
每一次调用,花费在函数及其衍生函数的平均时间microseconds
name                the name of the function. This is the minor sort
                         for this listing. The index shows the location of
                         the function in the gprof listing. If the index is
                         in parenthesis it shows where it would appear in
                         the gprof listing if it were to be printed.
                         
函数名

 

然后我们再来分析:

我们看到gprof默认会输出两组信息,即平台剖析数据(Flat profile)和调用图(Call graph).

我们只看平台剖析数据:

cumulative seconds:代表的是时间的累加,如上例,运行slowest函数(调用300万次)共用了0.01,此时cumulative seconds=0.01,

运行main函数用了0.05,此时cumulative seconds=0.15,运行slower函数(调用300万次)共用了0.04,此时cumulative seconds=0.19,

最后slow函数(调用300万次)共用了0.04,最后cumulative seconds=0.23.

self seconds:代表的是函数本身的运行时间,比如slowest它占用的时间最长,被调用300万次,共用了0.10.

 

 

### 使用 gprof 进行性能分析及其与 bch 的关联 #### 什么是 gprof? `gprof` 是 GNU Profiler 工具,用于程序的性能分析。它能够提供关于函数调用次数、执行时间以及其他统计信息的数据[^1]。 #### 如何使用 gprof? 为了使用 `gprof` 对程序进行性能分析,需要遵循以下方法: 1. **编译程序时启用 profiling 支持** 编译源代码时需加上 `-pg` 参数以便生成可供 `gprof` 分析的信息文件。 ```bash gcc -o my_program my_program.c -pg ``` 2. **运行程序并生成数据文件** 当带有 `-pg` 参数编译后的可执行文件被运行时,会自动生成名为 `gmon.out` 的文件,该文件包含了程序运行期间的性能统计数据[^2]。 3. **分析数据** 使用 `gprof` 命令读取 `gmon.out` 文件来获取详细的性能报告。 ```bash gprof ./my_program gmon.out > analysis.txt ``` 此命令将把分析结果重定向到 `analysis.txt` 中,便于查看和保存。 #### 关于 bch 和 gprof 的关系 `bch` 并不是标准术语或者工具名称,在此上下文中可能指的是某种特定环境下的缓存命中率(Cache Hit Rate)。如果假设这里的 `bch` 表示的是缓存行为,则可以利用 `gprof` 来间接评估缓存效率的影响。通过观察不同部分代码的时间消耗分布情况,推测哪些地方可能存在频繁访问内存的情况从而影响缓存效果[^3]。 然而需要注意的是,`gprof` 主要关注 CPU 时间分配而非具体的硬件层面操作比如 L1/L2 cache 或者 branch prediction 等细节;因此对于深入研究诸如分支预测失败成本或者是更精细级别的存储器子系统表现来说,其他专门设计用来测量这些特性的工具可能会更加合适一些,例如 Intel VTune Amplifier XE, Perf Events (Linux), 或者 Oprofile 等高级剖析工具[^4]。 ```python def example_function(): pass # Replace with actual logic to profile. ```
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值