C:分析C程序的瓶颈

Joy*_*Joy 4 c debugging

我的C程序对效率至关重要.有些函数被称为数百万次,所以我想知道每个函数花了多少时间,给我这样的:

Total time: 100s
forward(): 20s;
align(): 15s;
...
others: 1s.
Run Code Online (Sandbox Code Playgroud)

是否有任何调试器可以执行此类分析?我在Ubuntu上使用Eclipse CDT,并使用gdb进行调试.有人建议Valgrind,但我找不到任何合适的.我发现有一些问题在讨论c#或php或perl profiling,对C的任何建议?谢谢.

===========================================

跟进:非常感谢所有的帮助,gprof似乎非常好.这是一个手动链接:http://www.cs.utah.edu/dept/old/texinfo/as/gprof_toc.html.

关于解释摘要的问题:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls  us/call  us/call  name    
 61.29      9.18     9.18                             bwa_print_sam_SQ
 21.96     12.47     3.29                             bwt_sa
  4.01     13.07     0.60                             bns_coor_pac2real
  3.87     13.65     0.58                             bwt_match_exact_alt
  2.60     14.04     0.39                             bwa_read_seq
  1.00     14.19     0.15                             bwt_match_gap
  0.80     14.45     0.12                             seq_reverse
Run Code Online (Sandbox Code Playgroud)

如果我没错,它说函数bwa_print_sam_SQ占总时间的61.29%.但我的程序运行96.24秒,此功能应该运行大约60秒.为什么列"累积"秒数仅为9.18?手册说:

cumulative seconds
    This is the cumulative total number of seconds the computer spent executing this functions, plus the time spent in all the functions above this one in this table. 
Run Code Online (Sandbox Code Playgroud)

我使用参数

"gprof -f pe_sai2sam_se_core -f bwa_print_sam_SQ -f seq_reverse ./peta > gprof", 
Run Code Online (Sandbox Code Playgroud)

函数"pe_sai2sam_se_core"在大循环中调用"bwa_print_sam_SQ".为什么报告说:

index % time    self  children    called     name
                                                 <spontaneous>
[1]     61.3    9.18    0.00                 bwa_print_sam_SQ [1]
-----------------------------------------------
                                                 <spontaneous>
[8]      0.8    0.12    0.00                 seq_reverse [8]
-----------------------------------------------
Run Code Online (Sandbox Code Playgroud)

它没有说pe_sai2sam_se_core ...为什么?

cni*_*tar 11

您不需要调试器.你需要什么叫做剖析器.既然你提到了Ubuntu,你可能想要开始gprof.

以下是您可以使用的方法gprof:

  • 禁用程序的所有编译器优化(-O0) - 当然是可选的
  • 添加-g-pg标志
  • 像往常一样重建并运行程序
  • 此时你的程序应该gmon.out在cwd中生成一个文件
  • 使用gprof检查数据:

    gprof ./your_program > prof
    
    Run Code Online (Sandbox Code Playgroud)

现在您可以查看该prof文件.它从平面轮廓开始,它简单地告诉你它在各种功能上花了多少时间.