11

I come from a Matlab background so I am used to a profiler which profiles every single line and not just every function like gprof or callgrind. Is there a profiler for C with a similar feature?

Thanks!

screenshot matlab profiler
(source: jburkardt at people.sc.fsu.edu)

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Framester
  • 33,341
  • 51
  • 130
  • 192
  • 1
    Many suggestions for profiling tools etc on SO already, see e.g. http://stackoverflow.com/questions/1168766/what-is-a-good-easy-to-use-profiler-for-c-on-linux – Paul R Jul 16 '10 at 09:49

5 Answers5

12

You can use use the GNU utility GCOV to do line by line profiling. Sample run from GCC Docs .

$ gcc -fprofile-arcs -ftest-coverage tmp.c
$ a.out
$ gcov tmp.c
  90.00% of 10 source lines executed in file tmp.c
  Creating tmp.c.gcov

The file tmp.c.gcov contains output like:

     -:    0:Source:tmp.c
     -:    0:Graph:tmp.gcno
     -:    0:Data:tmp.gcda
     -:    0:Runs:1
     -:    0:Programs:1
     -:    1:#include <stdio.h>
     -:    2:
     -:    3:int main (void)
     1:    4:{
     1:    5:  int i, total;
     -:    6:
     1:    7:  total = 0;
     -:    8:
    11:    9:  for (i = 0; i < 10; i++)
    10:   10:    total += i;
     -:   11:
     1:   12:  if (total != 45)
 #####:   13:    printf ("Failure\n");
     -:   14:  else
     1:   15:    printf ("Success\n");
     1:   16:  return 0;
     -:   17:}
mikek3332002
  • 3,546
  • 4
  • 37
  • 47
  • Unless I'm not reading the documentation correctly, this just tells you how many times each line executes, NOT how much time it takes to execute each line. Is there a way to use GCOV to figure out execution time per line? – Ben Jul 07 '14 at 18:55
  • @Ben You're correct, gcov counts the number of times each line has been executed. – mikek3332002 Jul 08 '14 at 00:25
4

I believe callgrind does that. I know it does cycle counts per line, but I'm not sure about 'time.'

Tony Stark
  • 24,588
  • 41
  • 96
  • 113
3

Shark, one of the profiling tools in Mac OS X, can do that (or even profile by instruction). I realise that your screenshot is on Windows so that may not be helpful, but perhaps you can run your code on a Mac. You could try Very Sleepy, but I've never used it so have no idea how good it is.

  • 1
    +1 for Shark - the nearest equivalent on Linux is Zoom (www.rotateright.com) and I think the best you can do on Windows is Intel's VTune (www.intel.com). – Paul R Jul 16 '10 at 09:46
  • Zoom looks like the thing I want, but unfortunately I have no budget for a 200$ profiler. – Framester Aug 18 '10 at 16:06
0

Check this link and try this method.

The trouble with an example like Mandelbrot is that it is not a very big program. In real-world software the call tree gets much deeper and way more bushy, so you need to find out, per line or instruction, what percent of time it is responsible for, and that is just the percent of time it is on the call stack. So, you need something that samples the call stack and tells you, for each line or instruction that appears there, what percent of samples it is on. You don't need high precision of measurement - that is one of the myths.

There are tools that do this, one is RotateRight/Zoom, and another is LTProf. Personally I swear by the totally manual method.

Over the last couple days, we had a performance problem in some code around here. By the manual method, I found one way to save 40%. Then I found a way to save 40% on top of that, for a total saving of 64%. That's just one example. Here's an example of saving over 97%.

Added: There are social implications of this that can limit the potential speedup. Suppose there are three problems. Problem A (in your code) takes 1/2 of the time. Problem B (in Jerry's code) takes 1/4 of the time, and problem C (in your code) takes 1/8 of the time. When you sample, problem A jumps out at you, and since it's your code, you fix it, and now the program takes 1/2 the original time. Then you sample again, and problem B (which is now 1/2) jumps out at you. You see that it is in Jerry's code, so you have to explain it to Jerry, trying not to embarrass him, and ask him if he could fix it. If he doesn't for whatever reason (like that was some of his favorite code) then even if you fix problem C, time could only be reduced to 3/8 of the original time. If he does fix it, you can fix C and get down to 1/8 of the original time. Then there could be another problem D (yours) that if you fix it could get the time down to 1/16 of the original time, but if Jerry doesn't fix problem B you can't do any better than 5/16. That is how social interaction can be absolutely critical in performance tuning.

The only technique I've seen that works (because it was used on me) is to present the information in a sorrowful, apologetic tone, as if it were your problem, and be persistent about presenting the information. The apologetic tone defuses the embarassment, and the persistence keeps him thinking about it.

Community
  • 1
  • 1
Mike Dunlavey
  • 40,059
  • 14
  • 91
  • 135
-1

Our SD C Profiler tool works with GCC source code. It provides profiling of basic blocks rather than lines; this gives the same exact information with considerably lower overhead.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • 1
    It appears this answer is getting spam-flagged. It would be helpful if those that are doing would state their explicit objection. It would helpful if those of you that think this answer is useful left a specific comment as to why it is relevant. – Ira Baxter Aug 18 '10 at 19:41
  • You've been doing this crap for at least 6 years? – xaxxon Nov 06 '16 at 07:58
  • Doing what crap? I've been building tools for 40+ years. You mean answering questions needing or requesting tools, with appropriate tools? Somehow that doesn't seem wrong. You seem to be hostile to anything that isn't free. – Ira Baxter Nov 06 '16 at 14:19
  • @xaxxon: Do I have this right: you are downvoting because the answer does not address the OP's question, and/or the answer is wrong? – Ira Baxter Nov 06 '16 at 16:49
  • low effort self advertising spam. – xaxxon Nov 06 '16 at 22:47
  • I see you did not object to the factual or utilitarian nature of the answer, only to a violation of your world view. You can have your opinions, but you can't have your own facts. This kind of answer is accepted at SO as useful by policy long hashed out. – Ira Baxter Nov 06 '16 at 23:10