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 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:
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. |
|||||||
|
|
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. 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. |
||||
|
|
|
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
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 |
|||
|
|
This is a very bad idea, because there are already tools to do that. On linux, there is Also on linux, you can use massif, which is a heap profiler. |
|||
|
|
|
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. |
|||
|
|
|
Space: Use specialized allocator(s). Time: Agner is a good reference. http://www.agner.org/optimize/ |
|||
|
|
|
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. |
|||||||||
|