How to accurately measure the run time of a function in C++

Asked

Viewed 161 times

2

I am doing a college job and need to measure the run time of a recursive function, but when I do the measurement in nanoseconds most of my data comes back zeroed.

Is there any way to do a more accurate measurement in C++ ? Attached is how I did using the library chrono:

auto t1 = chrono::high_resolution_clock::now();
resultado = fiboRec(n);
auto t2 = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::nanoseconds>( t2 - t1 ).count();
cout << "O numero de Fibonacci na posicao " << n << " eh "<< resultado;
cout << "O tempo gasto foi de " << duration << " nanosegundos" << endl;

Some of the results obtained were as follows::

O numero de Fibonacci na posicao 20 eh 6765 O tempo gasto foi de 0 nanosegundos
O numero de Fibonacci na posicao 20 eh 6765 O tempo gasto foi de 1000000 nanosegundos
O numero de Fibonacci na posicao 22 eh 17711 O tempo gasto foi de 0 nanosegundos
O numero de Fibonacci na posicao 24 eh 46368 O tempo gasto foi de 999800 nanosegundos
O numero de Fibonacci na posicao 26 eh 121393 O tempo gasto foi de 0 nanosegundos
O numero de Fibonacci na posicao 28 eh 317811 O tempo gasto foi de 1998600 nanosegundos
  • It has come to wonder if really the processing is not fast enough not to be detected in the noticeable time interval for the library version chrono compiled on your machine? For example, what is the value of n for the verification?

  • Related: https://answall.com/q/258476/64969

  • I don’t know if I got it right, but there goes the exit I get from the program for some values Fibonacci number in position 20 eh 6765 The time spent was 0 nanoseconds Fibonacci number in position 20 eh 6765 The time spent was 1000000 nanoseconds Fibonacci number at position 22 eh 17711 Time spent was 0 nanoseconds Fibonacci number at position 24 eh 46368 Time spent was 999800 nanoseconds Fibonacci number at position 26 eh 121393 Time spent was 0 nanoseconds Fibonacci number in position 28 eh 317811 The time spent was 1998600 nanoseconds

  • a possible problem may be that my computer is saving the answer and this is affecting the time it takes to calculate, but I don’t know how to solve it

  • There is no way the computer is storing the function return data. In this case it is not caching an http response. He would only do it by chance you had taught

  • 1

    Another thing your performance experiment isn’t taking into account are random factors. In a multi-processed system, each processing core can only run one thread at a time. This means that if the core that was processing your program is diverted to another activity (and this is common, it prevents the other process from "starving to death"), the time the processing core spent running the another program will be taken into account in your count. Not to mention random fluctuations are also not being ruled out...

  • 1

    One of the most common ways of discarding these fluctuations of the experiment that you don’t have full control of is to run it over and over and get an average. It is linked in my second comment things on the subject. It’s important to read the question I said is related and also the links within it to get a better idea of where you’re walking when testing performance

  • For such small times usually the throughput (number of operations in a certain amount of time) is more important than the time of each individual operation. The throughput It also gives you a good idea of the average of the individual items (just divide the amount of time by the amount of operations). In this sense it is worth using a library as Google benchmark to calculate the throughput and avoid most of the traps mentioned by Jefferson Quesado.

Show 3 more comments

1 answer

0

If you are compiling for windows you can try Queryperformancecounter, but I don’t believe you can achieve such a significant improvement.

Then the best option is to run the function N times and take an average.

*Obs: the numbers will never be absolute, will be relative to the presence of other processes disputing the RAM and the CPU clock, which will increase/decrease according to use.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.