Design a CPU Timer class (you may name it CPUTimer). This class should allow the user to start timer, stop timer, compute delta time and print the lapsed CPU time of any block of code in a program.
#pragma once
#include <iostream>
#include <cmath>
#include <ctime>
#ifndef CPUTimer_h
#define CPUTimer_h
using namespace std;
class CPUTimer
{
private:
long t0;
long t1;
public:
CPUTimer(void);
~CPUTimer(void);
void startSetT0();
long getT0();
void stopSetT1();
long getT1();
void printTimeConsumed ();
};
#endif
int main ()
{
CPUTimer counter;
counter.startSetT0();
for(float i=0; i<10000000; ++i)
pow(i,i) * sqrt(i*i) / exp(i);
counter.stopSetT1();
counter.getT1();
counter.printTimeConsumed();
return 0;
}
CPUTimer::CPUTimer(void)
{
}
CPUTimer::~CPUTimer(void)
{
}
void CPUTimer::startSetT0()
{
t0=clock();
}
void CPUTimer::stopSetT1()
{
t1 = clock ();
}
long CPUTimer:: getT0(){
return t0;
}
long CPUTimer::getT1(){
return t1-t0;
}
void CPUTimer::printTimeConsumed ()
{
cout << "CPU times in ms below... " << endl << endl;
cout << "CPU start time in ms ===> " << t0 << endl;
cout << "CPU stop time in ms ====> " << t1 << endl;
cout << "CPU running time change in ms ===>The change in CPU running time in ms = " << t1-t0 << endl << endl;
system("pause");
};