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)
How can I measure the performance of my C++ programs using C++?
Specific metrics I want to measure are:
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
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.
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:
MyClass::myMethod()
{
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.
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.
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.
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.
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.