title: C: analyze bottleneck of C programs
body: <p>My C program is efficiency critical. 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:</p>
  
  <pre><code>Total time: 100s
  forward(): 20s;
  align(): 15s;
  ...
  others: 1s.
  </code></pre>
  
  <p>Is there any debugger can perform such analysis? I am using Eclipse CDT on Ubuntu, and using gdb to debug. Some guy suggested <a href="http://valgrind.org/" rel="nofollow">Valgrind</a>, but I did not find any suitable. I found there are some questions talking about c# or php or perl profiling, any suggestions for C? Thanks.</p>
  
  <p>===========================================</p>
  
  <p>Follow up: thanks very much for all help,  gprof seems really nice. Here is a manual link: <a href="http://www.cs.utah.edu/dept/old/texinfo/as/gprof_toc.html" rel="nofollow">http://www.cs.utah.edu/dept/old/texinfo/as/gprof_toc.html</a>. </p>
  
  <p>A question about interpreting the summary:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>If I am not wrong, it says the function bwa_print_sam_SQ takes 61.29% of the total time. But my program runs for 96.24 seconds, this function should run around 60 seconds. Why the column "cumulative" seconds is only 9.18? The manual says:</p>
  
  <pre><code>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. 
  </code></pre>
  
  <p>And I use the parameter </p>
  
  <pre><code>"gprof -f pe_sai2sam_se_core -f bwa_print_sam_SQ -f seq_reverse ./peta &gt; gprof", 
  </code></pre>
  
  <p>where function "pe_sai2sam_se_core" calls "bwa_print_sam_SQ" in a big while loop. Why the report says:</p>
  
  <pre><code>index % time    self  children    called     name
                                                   &lt;spontaneous&gt;
  [1]     61.3    9.18    0.00                 bwa_print_sam_SQ [1]
  -----------------------------------------------
                                                   &lt;spontaneous&gt;
  [8]      0.8    0.12    0.00                 seq_reverse [8]
  -----------------------------------------------
  </code></pre>
  
  <p>It did not say anything about pe_sai2sam_se_core... Why?</p>