gprof

本文介绍了gprof工具的使用方法及如何通过它分析程序性能。gprof可以生成程序运行的flat profile,显示函数调用次数与处理器时间消耗,并提供调用图和注释源代码等信息帮助开发者优化程序。
gprof介绍
gprof是GNU profiler工具。可以显示程序运行的“flat profile”,包括每个函数的调用次数,每个函数消耗的处理器时间。也可以显示“调用图”,包括函数的调用关系,每个函数调用花费了多少时间。还可以 显示“注释的源代码”,是程序源代码的一个复本,标记有程序中每行代码的执行次数。

为gprof编译程序
在编译或链接源程序的时候在编译器的命令行参数中加入“-pg”选项,编译时编译器会自动在目标代码中插入用于性能测试的代码片断,这些代码在程序在运行 时采集并记录函数的调用关系和调用次数,以及采集并记录函数自身执行时间和子函数的调用时间,程序运行结束后,会在程序退出的路径下生成一个 gmon.out文件。这个文件就是记录并保存下来的监控数据。可以通过命令行方式的gprof或图形化的Kprof来解读这些数据并对程序的性能进行分 析。另外,如果想查看库函数的profiling,需要在编译是再加入“-lc_p”编译参数代替“-lc”编译参数,这样程序会链接libc_p.a 库,才可以产生库函数的profiling信息。如果想执行一行一行的profiling,还需要加入“-g”编译参数。
例如如下命令行:
gcc -Wall -g -pg -lc_p example.c -o example

执行gprof
执行如下命令行,即可执行gprof:
gprof OPTIONS EXECUTABLE-FILE gmon.out BB-DATA [YET-MORE-PROFILE-DATA-FILES...] [> OUTFILE]

gprof产生的信息
 %                        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.
                          每一次调用花费在函数的时间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.
                          函数名

在你运行了你的程序并产生了 gmon.out 文件后你能用下面的命令获得剖析信息:

gprof ./program_name

参数 program_name 是产生 gmon.out 文件的程序的名字。

### 使用 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. ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值