简体   繁体   English

C:分析C程序的瓶颈

[英]C: analyze bottleneck of C programs

My C program is efficiency critical. 我的C程序对效率至关重要。 Some functions are called millions of times, so I would like to know how much time is spent on each function, giving me sth like this: 有些函数被称为数百万次,所以我想知道每个函数花了多少时间,给我这样的:

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

Is there any debugger can perform such analysis? 是否有任何调试器可以执行此类分析? I am using Eclipse CDT on Ubuntu, and using gdb to debug. 我在Ubuntu上使用Eclipse CDT,并使用gdb进行调试。 Some guy suggested Valgrind , but I did not find any suitable. 有人建议Valgrind ,但我找不到任何合适的。 I found there are some questions talking about c# or php or perl profiling, any suggestions for C? 我发现有一些问题在讨论c#或php或perl profiling,对C的任何建议? Thanks. 谢谢。

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

Follow up: thanks very much for all help, gprof seems really nice. 跟进:非常感谢所有的帮助,gprof似乎非常好。 Here is a manual link: http://www.cs.utah.edu/dept/old/texinfo/as/gprof_toc.html . 这是一个手动链接: http//www.cs.utah.edu/dept/old/texinfo/as/gprof_toc.html

A question about interpreting the summary: 关于解释摘要的问题:

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

If I am not wrong, it says the function bwa_print_sam_SQ takes 61.29% of the total time. 如果我没错,它说函数bwa_print_sam_SQ占总时间的61.29%。 But my program runs for 96.24 seconds, this function should run around 60 seconds. 但我的程序运行96.24秒,此功能应该运行大约60秒。 Why the column "cumulative" seconds is only 9.18? 为什么列“累积”秒数仅为9.18? The manual says: 手册说:

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. 

And I use the parameter 我使用参数

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

where function "pe_sai2sam_se_core" calls "bwa_print_sam_SQ" in a big while loop. 函数“pe_sai2sam_se_core”在大循环中调用“bwa_print_sam_SQ”。 Why the report says: 为什么报告说:

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]
-----------------------------------------------

It did not say anything about pe_sai2sam_se_core... Why? 它没有说pe_sai2sam_se_core ...为什么?

You don't need a debugger. 您不需要调试器。 What you need is called a profiler. 你需要什么叫做剖析器。 Since you mention Ubuntu, you probably want to start with gprof . 既然你提到了Ubuntu,你可能想从gprof开始。

Here's how you can use gprof : 以下是如何使用gprof

  • Disable all compiler optimizations for your program ( -O0 ) - optional of course 禁用程序的所有编译器优化( -O0 ) - 当然是可选的
  • Add the -g and the -pg flags 添加-g-pg标志
  • Rebuild and run the program as usual 像往常一样重建并运行程序
  • At this point your program should have produced a gmon.out file in the cwd 此时你的程序应该在cwd中生成一个gmon.out文件
  • Use gprof to inspect data: 使用gprof检查数据:

     gprof ./your_program > prof 

Now you can view the prof file. 现在您可以查看prof文件。 It begins with the flat profile which simply tells you how much time it's spending in various functions. 它从平面轮廓开始,它简单地告诉你它在各种功能上花了多少时间。

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

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