Avoiding CPU Contention

105 views Asked by At

I have a program that I want to calculate its time of execution :

#include <iostream>
#include <boost/chrono.hpp>

using namespace std;

int main(int argc, char* const argv[])
{


     boost::chrono::system_clock::time_point start = boost::chrono::system_clock::now();

// Intructions to burn time 

    boost::chrono::duration<double> sec = boost::chrono::system_clock::now() - start;
    cout <<"---- time execution is " << sec.count() << ";";
    return 0;
}

For example the result after one run:

---- time execution is 0.0223588

This result isn't very conscious because the CPU time is included .

I had An idea to avoid CPU contention by testing many runs and getting there average .

The problem is :

  • How can I store the time value of the previous run ?

  • Can we do that via a file ?

  • How to incrementally calculate the average after each run ?

Your suggestion / pseudocodes are welcome.

1

There are 1 answers

0
Alexander Dyagilev On BEST ANSWER

You may pass average number via command line using 2 args: current average value and the number of iterations performed. Then: NewAverage = ((CurrentAverage*N) + CurrentValue) / (N+1); where N is the number of iterations.