This time measure routine uses
GetTickCount() function of Windows. Thought it does not give exact CPU cycles of the code fragment, you can get a rough real-life measurement from this. Time measure routine using
GetTickCount() is shown below.
#include <iostream>
#include <Windows.h>
using namespace std;
unsigned long startTime_;
void startTime()
{
startTime_ = GetTickCount();
}
unsigned int calculateElapsedTime()
{
unsigned int diffInMilliSeconds = GetTickCount() - startTime_;
return diffInMilliSeconds;
}
int main()
{ timeBeginPeriod(1); unsigned int diffTime = 0, lastTime = 0, newTime = 0;
startTime();
cout<<"Start Time = "<<calculateElapsedTime()<<endl;
Sleep(100);
newTime = calculateElapsedTime();
diffTime = newTime - lastTime;
cout<<"Time after 100ms Sleep = "<<newTime<<", Difference = "<<diffTime<<endl;
lastTime = newTime;
Sleep(100);
newTime = calculateElapsedTime();
diffTime = newTime - lastTime;
cout<<"Time after 100ms Sleep = "<<newTime<<", Difference = "<<diffTime<<endl;
lastTime = newTime;
Sleep(5);
newTime = calculateElapsedTime();
diffTime = newTime - lastTime;
cout<<"Time after 5ms Sleep = "<<newTime<<", Difference = "<<diffTime<<endl;
lastTime = newTime;
Sleep(50);
newTime = calculateElapsedTime();
diffTime = newTime - lastTime;
cout<<"Time after 50ms Sleep = "<<newTime<<", Difference = "<<diffTime<<endl;
timeEndPeriod(1); return 0;
}