How can I measure the performance of my C++ programs using C++?

Specific metrics I want to measure are:

  • Memory used (space)
  • Duration taken (time)
  • 6
    Using C++ to do this seems like a bad idea. Why not just use a standard profiler which gives better results for less work? – Pubby Apr 14 '12 at 7:05
  • It would help if you could specify why you are limited to measuring performance using c++. – Paul Hiemstra Apr 14 '12 at 7:10
  • (1) Measuring peak memory and CPU usage by the whole application? (Use an OS-monitoring tool.) (2) Measure the variations of memory and CPU usage by each and every piece of code in a program? (Use a profiler.) (3) Measure the memory and CPU usage of a small piece of code, while being executed realistically in the middle of everything else in the application? (Insert your own "instrumentation code" to calculate memory and time usage.) – rwong Sep 21 '13 at 20:00

For the profiling side of things, as long as you are using GNU, you can use gprof. It will give you results like this:

Each sample counts as 0.01 seconds.

  %   cumulative   self              self     total           
 time   seconds   seconds    calls  ms/call  ms/call  name    
 33.34      0.02     0.02     7208     0.00     0.00  open
 16.67      0.03     0.01      244     0.04     0.12  offtime
 16.67      0.04     0.01        8     1.25     1.25  memccpy
 16.67      0.05     0.01        7     1.43     1.43  write
 16.67      0.06     0.01                             mcount
  0.00      0.06     0.00      236     0.00     0.00  tzset
  0.00      0.06     0.00      192     0.00     0.00  tolower
  0.00      0.06     0.00       47     0.00     0.00  strlen
  0.00      0.06     0.00       45     0.00     0.00  strchr
  0.00      0.06     0.00        1     0.00    50.00  main
  0.00      0.06     0.00        1     0.00     0.00  memcpy
  0.00      0.06     0.00        1     0.00    10.11  print
  0.00      0.06     0.00        1     0.00     0.00  profil
  0.00      0.06     0.00        1     0.00    50.00  report

Link To data

THe great thing about this is that it has all functions in the system traced so that you get an accurate view of which function to look at.

| improve this answer | |
  • "The great thing about this is that"... what? – A T Apr 15 '12 at 3:32

For duration (time) I would also use a profiler. However, if you want to track certain methods in particular, I have used the trick of defining a small timer class that starts a timer in the constructor and stops it in the destructor. Then all you need to do is to define a local timer variable at the beginning of the methods you want to profile, like this:

    MyTimer timer("MyClass::myMethod");


The destructor of the timer variable will log the name of the method and the duration when you exit the method.

Yes, using a profiler is much cleaner and you don't need to changed the code (even though I normally add the timer variables in a separate copy of the source code so no clean-up is needed afterwards) but I found this method an effective alternative to using a profiler if the code you want to profile is very localized.

Just my 2 cents.

| improve this answer | |
  • 1
    actually, this is the best answer to the asked question. To check space, you'd use a custom allocator (ie override new and delete) – gbjbaanb Apr 15 '12 at 16:07
  • @gbjbaanb: I once wanted to override new and delete to profile memory usage and memory leaks but I do not know how to do it. Is there a standard way of doing this, or does it depend on the compiler one uses? – Giorgio Apr 15 '12 at 20:29

I agree with Pubby that profiling using external tools is better. Here are some pointers:

To get a rough estimate of the time an executable takes, I would use a linux tool like time. Running the executable spam like:

time spam

Gives you feedback on how long it took. You could also write a small Python script which does this a few times and averages the results. See this SO thread for some hints how to measure the memory used by an application.

But this gives only a cumulative view of your program. Much more interesting is to break this analysis up for different parts of your code. You can do this by profiling your code, for example using the GNU profiler gprof. This will present you an overview of how much time is spent in which parts of the code. This can grant you insight where you could spend more time optimzing, and where the performance of the code is not really important.

| improve this answer | |

How can I measure the performance of my C++ programs using C++?

This is a very bad idea, because there are already tools to do that.

On linux, there is time to measure time used by application.

Also on linux, you can use massif, which is a heap profiler.

| improve this answer | |

For measuring space, I'll defer to other answers.

For time, I have to ask, are you measuring just because you want to measure, or because you want to make the program take less time?

I only ask because measuring time, even of individual functions, does not tell what you should fix to make it run faster. It may tell you where you shouldn't look (i.e. functions with low inclusive percent), but that doesn't tell you where you should concentrate. To do that, here's the method I use.

| improve this answer | |
  • I am measuring because I am analysing the performance of different data-structures. – A T Apr 14 '12 at 16:10
  • @AT: Then what I would do is count basic operations, not measure time, because the same algorithm can easily differ between different implementations by orders of magnitude, as demonstrated here. – Mike Dunlavey Apr 14 '12 at 19:03
  • But each operation costs a different amount depending on its implementation. Besides, when I'm developing a new data-structure is it important that I use industry-standard metrics. – A T Apr 15 '12 at 3:34
  • @AT: Industry-standard metrics? What are those? – Mike Dunlavey Apr 15 '12 at 16:08

Space: Use specialized allocator(s). This way, you can record exact memory consumption and events -- local to the implementation you are interested in measuring. For example: std::vector<int,t_your_allocator>. Global new/delete (for testing only) and new placement are other approaches.

Time: Agner is a good reference. http://www.agner.org/optimize/ Agner Fog goes deep into costs and measurement in writing -- several books (or manuals) are available. The site also hosts implementations which measure execution times.

| improve this answer | |

I am sure you must have a very good reason to profile your C++ program using another C++ program. If you are in Linux, I suggest you look into details of the /proc filesystem and files like meminfo which the OS maintains tracking your runtime memory usage.

If you are in the mood to explore tools that can take a lot of hard work to develop on your own I suggest you look into Quantify, VTune, Valgrind etc.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.