使用 gprof 分析优化的 C 代码

计算科学 优化 C 分析
2021-12-16 08:05:39

我有一个包含许多函数调用的简单 C 代码,我使用 gprof 对其进行了分析。

%   cumulative   self              self     total           
 time   seconds  seconds    calls   s/call   s/call  name    
71.93    907.85   907.85   201280     0.00     0.00  ForceCalcs
 2.61   1237.18    33.00     1258     0.03     1.00  Heap
 1.53   1256.45    19.28     1258     0.02     0.02  AdjustSP
 0.01   1261.61     0.07     1258     0.00     0.00  Boundary
 0.00   1261.61     0.00      100     0.00     0.00  Profile
 0.00   1261.61     0.00        2     0.00     0.00  MakeMap
 0.00   1261.61     0.00        1     0.00     0.18  Initialize

为了清楚起见,我从列表中省略了一些其他函数调用。这就是百分比加起来不等于 100 的原因。为了比较结果,我使用 -O3 命令对其进行编译并使用 gprof 再次对其进行分析。获得的平面轮廓如下:

% Each sample counts as 0.01 seconds.
% (--- means nothing mentioned)
%   cumulative   self              self     total 
 time   seconds  seconds    calls  ms/call  ms/call  name
73.66    460.88   460.88   201280     2.29     2.29  ForceCalc
 1.65    620.85    10.30    ---       ---      ---   Heap
 0.47    623.80     2.95    ---       ---      ---   AdjustSP
 0.00    626.10     0.01     1258     0.01     0.01  Boundary
 0.00    626.10     0.00        1     0.00     0.00  MakeMap

但是现在我无法理解我的代码中发生了哪些优化。例如

  1. 对 Initialize() 和 profile() 的调用在后面的配置文件中没有对应的条目
  2. 在后面的配置文件中没有提到 Heap() 和 AdjustSP() 的调用或毫秒/调用。(这就是我想通过 --- 展示的内容)

有人可以解释上述2个观察的含义。我认为它与内联有关,但不确定。

1个回答

有两件事可能有助于理解正在发生的事情:

1) gprof 的输出有时难以解析

有一个工具可以帮助获得称为 gprof2dot 的图形表示(与 dot 结合使用如下):

gprof 可执行文件 | gprof2dot.py | 点 -T pdf > profile.pdf

在哪里

2) 结合 gprof 使用系统范围的分析器

例如,在 Linux 下,您可以使用缩放:http ://www.rotateright.com/zoom-profiler/

它不是开源的,而是免费提供的。