繁体   English   中英

C:分析C程序的瓶颈

[英]C: analyze bottleneck of C programs

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

Total time: 100s
forward(): 20s;
align(): 15s;
...
others: 1s.

是否有任何调试器可以执行此类分析? 我在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

如果我没错,它说函数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. 

我使用参数

"gprof -f pe_sai2sam_se_core -f bwa_print_sam_SQ -f seq_reverse ./peta > gprof", 

函数“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]
-----------------------------------------------

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

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

以下是如何使用gprof

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

     gprof ./your_program > prof 

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM