如何修改C程序以便gprof可以对其进行分析?

neu*_*cer 4 c linux profiler profiling gprof

当我在我的C程序上运行gprof时,它表示我的程序没有累积时间,并显示所有函数调用的0时间.但它会计算函数调用.

如何修改我的程序,以便gprof能够计算运行所需的时间?

小智 16

编译时是否指定了-pg?

http://sourceware.org/binutils/docs-2.20/gprof/Compiling.html#Compiling

编译完成后,运行该程序,然后在二进制文件上运行gprof.

例如:

test.c的:

#include <stdio.h>

int main ()
{
    int i;
    for (i = 0; i < 10000; i++) {
        printf ("%d\n", i);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译为cc -pg test.c,然后运行a.out,然后gprof a.out,给我

granularity: each sample hit covers 4 byte(s) for 1.47% of 0.03 seconds

  %   cumulative   self              self     total           
 time   seconds   seconds    calls  ms/call  ms/call  name    
 45.6       0.02     0.02    10000     0.00     0.00  __sys_write [10]
 45.6       0.03     0.02        0  100.00%           .mcount (26)
  2.9       0.03     0.00    20000     0.00     0.00  __sfvwrite [6]
  1.5       0.03     0.00    20000     0.00     0.00  memchr [11]
  1.5       0.03     0.00    10000     0.00     0.00  __ultoa [12]
  1.5       0.03     0.00    10000     0.00     0.00  _swrite [9]
  1.5       0.03     0.00    10000     0.00     0.00  vfprintf [2]

你得到了什么?

  • @Phenom:在这种情况下,根据您选择分享的内容,我没有任何建议.你需要告诉我们的不仅仅是它是一个用`-pg`编译的C程序,而且你得到了函数计数但没有CPU时间.你确定你正在消耗CPU时间,而不是通过I/O调用运行挂钟时间吗? (2认同)