1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#include "gstopwatch.h"
#ifdef _WIN32
double GStopWatch::LIToSecs( LARGE_INTEGER & L) {
return ((double)L.QuadPart /(double)frequency.QuadPart);
}
GStopWatch::GStopWatch(){
timer.start.QuadPart=0;
timer.stop.QuadPart=0;
QueryPerformanceFrequency( &frequency );
}
void GStopWatch::startTimer( ) {
QueryPerformanceCounter(&timer.start);
}
void GStopWatch::stopTimer( ) {
QueryPerformanceCounter(&timer.stop);
}
double GStopWatch::getElapsedTime() {
LARGE_INTEGER time;
time.QuadPart = timer.stop.QuadPart - timer.start.QuadPart;
return LIToSecs( time) ;
}
#else
//Linux code:
void GStopWatch::startTimer( ) {
gettimeofday(&(timer.start),NULL);
}
void GStopWatch::stopTimer( ) {
gettimeofday(&(timer.stop),NULL);
}
double GStopWatch::getElapsedTime() {
timeval res;
timersub(&(timer.stop),&(timer.start),&res);
return res.tv_sec + res.tv_usec/1000000.0; // 10^6 uSec per second
}
#endif
|