Friday, February 15, 2013

Program running time

#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <windows.h>
using namespace std;
void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}
int main()
{
clock_t t1,t2;
t1=clock();
int i =0;
while(i<10)
{
i++;
//wait(1);
Sleep(1000);
cout<<i<<endl;
}
t2=clock();
long diff = t2-t1;
float seconds = (float)diff / CLOCKS_PER_SEC;
cout<<"Clock Diffrence:>>"<<diff<<endl;// clock difference
cout<<"Time Difference:>>"<<seconds<<endl;// time difference in seconds
system ("pause");
return 0;
}
view raw gistfile1.cpp hosted with ❤ by GitHub
Sleep should be used instead of wait(). Because wait takes too much CPU usage.


Measure Time in OpenCV

two simple functions to achieve this getTickCount() and getTickFrequency(). The first returns the number of ticks of your systems CPU from a certain event (like since you booted your system). The second returns how many times your CPU emits a tick during a second. So to measure in seconds the number of time elapsed between two operations is easy as:
double t = (double)getTickCount();
// do something ...
t = ((double)getTickCount() - t)/getTickFrequency();
cout << "Times passed in seconds: " << t << endl;

No comments:

Post a Comment