1

I need to measure the time of a C++ programs, especially the overall running time of some recursive functions. There are a lot of function calls inside other functions. My first thought was to implement some time measurements functions in the actual code.

The problem with gprof is, that it prints out the time of class operators of a datatype, but i only need the infomartion about the functions and "-f func_name prog_name" wont work.

So, what is the most common way in science to measure time of a numerical program?

Its something like this:

function2()
{
}
function1()
{
  function2();
  funtcion1();
}
int main(){
function1();
}
Biler
  • 41
  • 1
  • 1
  • 6
  • The code you have here is an infinite recursion. I'll give you an answer if you can define what you mean by "overall running time of some recursive functions". For example: do you mean the time that would be saved if function F could be made infinitely fast? [*Don't expect gprof to give you a sensible answer for recursive functions.*](http://stackoverflow.com/a/1779343/23771) – Mike Dunlavey Oct 26 '13 at 17:01
  • time taken to execute a complete function http://stackoverflow.com/a/40380118/6180077 – Abdullah Farweez May 17 '17 at 05:05

2 Answers2

2

If you're using the GNU package, i.e. gcc, you can try gprof. Just compile your program with -g and -pg flags and then run gprof <your_program_name>

gprof: http://www.cs.utah.edu/dept/old/texinfo/as/gprof_toc.html

EDIT: In order to increase the level of detail you can run gprof with other flags:

-l (--line) enables line by line profiling, giving you a histogram hits to be charged to individual lines of code, instead of functions.

-a Don’t include private functions in the output.

-e <function> Exclude output for a function <function>. Use this when there are functions that won’t be changed. For example, some sites have source code that’s been approved by a regulatory agency, and no matter how inefficient, the code will remain unchanged.

-E <function> Also exclude the time spent in the function from the percentage tables.

-f <function> The opposite of -e: only track time in <function>.

-F <function> Only use the time in <function> when calculating percentages.

-b Don’t print the explanatory text. If you’re more experienced, you can appreciate this option.

-s Accumulate samples. By running the program several times, it’s possible to get a better picture of where time is spent. For example, a slow routine may not be called for all input values, and therefore you maybe mislead reading where to find performance problems.

Sebastian Kramer
  • 689
  • 1
  • 9
  • 18
1

If you need higher precision (for functions which do not take more than few (or less) milliseconds), you can use std::chrono::high_resolution_clock:

auto beginT = std::chrono::high_resolution_clock::now();
//Your computation here
auto endT = std::chrono::high_resolution_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(endT - beginT).count()

The std::chrono::high_resolution_clock can be found in chrono header and is part of C++11 stadard.

N A
  • 656
  • 3
  • 6
  • Thx, but actually I dont need higher precision – Biler Oct 20 '13 at 16:11
  • @Biler It works perfectly for low precision as well and it is part of STL. You can cast the result to whatever time unit you need which is very convenient. – N A Oct 20 '13 at 16:16
  • I have to save the data in a variable and use this as a function parameter to sum up the time for the overall function calls. – Biler Oct 20 '13 at 16:41
  • @Biler If you need to do the measurement inside function then i am afraid the only solution would be to add `bool` to the parameter list which denotes whether the time shall be measured or not. The function would then - when it calls itself - just pass false so the time is not measured inside all the recursions. The user application would the pass on true which would cause time measurement on the first call of the function. This way you would not need to sum everything up – N A Oct 20 '13 at 17:05
  • Thx, but please take a look of my first post. Thats the situation and i dont know how i can get the overall time measurement for every function. So that i can print in the main function the run times of function1, function2, function3 – Biler Oct 20 '13 at 17:10
  • @Biler Sorry, I thought you needed time just for all of the recursive functions together. In this case yes, you would need to add up all the times, if you want time of just one recursion. Are you sure you cant find way to avoid recursion? Many implementations can be changed to avoid it. In your case doing so will solve your time measurement issues. – N A Oct 20 '13 at 17:28
  • I simplified it...and used clock() for time measurement. – Biler Oct 21 '13 at 10:23
  • @Biler If you have found solution, it is good practice here on SO to post it and accept it so the thread is marked as solved (unless you want to mark the most useful answer instead). – N A Oct 21 '13 at 10:36