Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you profile your code?

Tags:

c

profiling

I hope not everyone is using Rational Purify.

So what do you do when you want to measure:

  • time taken by a function
  • peak memory usage
  • code coverage

At the moment, we do it manually [using log statements with timestamps and another script to parse the log and output to excel. phew...)

What would you recommend? Pointing to tools or any techniques would be appreciated!

EDIT: Sorry, I didn't specify the environment first, Its plain C on a proprietary mobile platform

like image 837
prakash Avatar asked Sep 11 '08 14:09

prakash


2 Answers

I've done this a lot. If you have an IDE, or an ICE, there is a technique that takes some manual effort, but works without fail.

Warning: modern programmers hate this, and I'm going to get downvoted. They love their tools. But it really works, and you don't always have the nice tools.

I assume in your case the code is something like DSP or video that runs on a timer and has to be fast. Suppose what you run on each timer tick is subroutine A. Write some test code to run subroutine A in a simple loop, say 1000 times, or long enough to make you wait at least several seconds.

While it's running, randomly halt it with a pause key and sample the call stack (not just the program counter) and record it. (That's the manual part.) Do this some number of times, like 10. Once is not enough.

Now look for commonalities between the stack samples. Look for any instruction or call instruction that appears on at least 2 samples. There will be many of these, but some of them will be in code that you could optimize.

Do so, and you will get a nice speedup, guaranteed. The 1000 iterations will take less time.

The reason you don't need a lot of samples is you're not looking for small things. Like if you see a particular call instruction on 5 out of 10 samples, it is responsible for roughly 50% of the total execution time. More samples would tell you more precisely what the percentage is, if you really want to know. If you're like me, all you want to know is where it is, so you can fix it, and move on to the next one.

Do this until you can't find anything more to optimize, and you will be at or near your top speed.

like image 125
Mike Dunlavey Avatar answered Oct 21 '22 21:10

Mike Dunlavey


You probably want different tools for performance profiling and code coverage.

For profiling I prefer Shark on MacOSX. It is free from Apple and very good. If your app is vanilla C you should be able to use it, if you can get hold of a Mac.

For profiling on Windows you can use LTProf. Cheap, but not great: http://successfulsoftware.net/2007/12/18/optimising-your-application/

(I think Microsoft are really shooting themself in the foot by not providing a decent profiler with the cheaper versions of Visual Studio.)

For coverage I prefer Coverage Validator on Windows: http://successfulsoftware.net/2008/03/10/coverage-validator/ It updates the coverage in real time.

like image 25
Andy Brice Avatar answered Oct 21 '22 19:10

Andy Brice