Tutorial :Profiling programs written in C or C++


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).


I can heartily recommend callgrind in combination with KCachegrind.


"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.


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.


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.


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.


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


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).




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.


For performance measurements oprofile is a good choice as there exists a user friendly plugin for eclipse in the Linux Tools Project.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »