tags:

views:

571

answers:

9

What would you suggest the best tool to profile C/C++ code and determine which parts are taking the most time. Currently, I'm just relying on logs but ofcourse the information is not accurate since unnecessary delays are introduced.

Preferrably, the tool would also be able to detect/suggest areas which could be optimized, if such tool exist.

Platform: Linux

The application shall be used on an embedded environment so it should be lightweight and external (not a plugin on some IDE).

+9  A: 

I can heartily recommend callgrind in combination with KCachegrind.

Konrad Rudolph
kcachegrind is easily the best visualization i've seen, but beware that if you have the source, gprof will be much, much faster, and if performance is absolutely critical or you'd like to take a closer look at hardware performance counters to find pathologies, OProfile is likely the way to go.
Matt J
+2  A: 

I've made good experiences using the profiler from Microsoft Visual C++ , there are other external programs like Intels VTune, too, but most of them aren't free.

schnaader
+1  A: 

Depends on the platform. If you're using MSVC, some versions of it has a profiler built in. AMD and Intel both have profilers available as well (CodeAnalyst and VTune).

On Linux, the only one I've used is gprof, but I know there are others (and I think AMD's or Intels ones may be available in Linux versions too)

And of course the entire Valgrind suite may be helpful too. Some tools like callgrind or cachegrind can give you a lot of information about performance.

jalf
A: 

If you're on Windows, I suggest AQTime. Supports almost every compiler out there, including .NET, Delphi and VB (and all C++ compilers, of course (; ) and is just the best profiling tool I ever tried. And it's not only a performance profiler.

OregonGhost
+5  A: 

"gprof" on linux/freebsd is a quite simple and efficient tool to identify which routines are hogging the cPU at runtime. It gives both nested and flat profile of functions. It gives you the percentage of CPU time taken by each function executed during the runtime of the profiler, and also the percentage taken within the function itself, and the percentage taken by its child functions. That helps you easily segregate the offending functions.

Harty
That's all I needed last time I needed to improve performance.
David Thornley
+1  A: 

There are many good profiling tools for this like Quantify or KCachegrind. The one problem with these tools is that they slow down the run time performance so on some large systems they may not scale well enough.

Sometimes it is enough to just run in the debugger and press ctrl-c, look at the stack trace and repeat this maybe 4 times.

If you are always in the same part of the code then you have found where you are probably spending most of the time.

James Dean
Bingo. In particular, you should look for call sites that are on multiple call stack samples.
Mike Dunlavey
A: 

You clearly want two things:

  1. To profile your code & measure it.

  2. To detect areas that could be optimized.

These are different problems. They are not the same, in spite of what you may have been told.

For problem (1) many good profilers will be suggested.

For problem (2) profilers help only indirectly.
There is a much simpler and usually more effective technique.

Look Here

Mike Dunlavey
+1  A: 

Im gonna go with gprof / oprofile, if we are talking about the UNIX world.

You do need to recompile your app (at least with gprof).

Gprof

Oprofile

Tom
A: 

For performance measurements oprofile (http://oprofile.sourceforge.net) is a good choice as there exists a user friendly plugin for eclipse in the Linux Tools Project (http://www.eclipse.org/linuxtools/)

lothar