| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- //////////////////////////////////////////////////////////////////////////////
- // HighResolutionTimer.cpp
- // =========
- // High Resolution Timer.
- // This timer is able to measure the elapsed time with 1 micro-second accuracy
- // in both Windows, Linux and Unix system
- //
- // AUTHOR: Song Ho Ahn ([email protected])
- // CREATED: 2003-01-13
- // UPDATED: 2006-01-13
- //
- // Copyright (c) 2003 Song Ho Ahn
- //////////////////////////////////////////////////////////////////////////////
- #include "HighResolutionTimer.h"
- #include <stdlib.h>
- ///////////////////////////////////////////////////////////////////////////////
- // constructor
- ///////////////////////////////////////////////////////////////////////////////
- HighResolutionTimer::HighResolutionTimer()
- {
- #ifdef WIN32
- QueryPerformanceFrequency(&frequency);
- startCount.QuadPart = 0;
- endCount.QuadPart = 0;
- #else
- startCount.tv_sec = startCount.tv_usec = 0;
- endCount.tv_sec = endCount.tv_usec = 0;
- #endif
- stopped = 0;
- startTimeInMicroSec = 0;
- endTimeInMicroSec = 0;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // distructor
- ///////////////////////////////////////////////////////////////////////////////
- HighResolutionTimer::~HighResolutionTimer()
- {
- }
- ///////////////////////////////////////////////////////////////////////////////
- // start timer.
- // startCount will be set at this point.
- ///////////////////////////////////////////////////////////////////////////////
- void HighResolutionTimer::start()
- {
- stopped = 0; // reset stop flag
- #ifdef WIN32
- QueryPerformanceCounter(&startCount);
- #else
- gettimeofday(&startCount, NULL);
- #endif
- }
- ///////////////////////////////////////////////////////////////////////////////
- // stop the timer.
- // endCount will be set at this point.
- ///////////////////////////////////////////////////////////////////////////////
- void HighResolutionTimer::stop()
- {
- stopped = 1; // set timer stopped flag
- #ifdef WIN32
- QueryPerformanceCounter(&endCount);
- #else
- gettimeofday(&endCount, NULL);
- #endif
- }
- ///////////////////////////////////////////////////////////////////////////////
- // compute elapsed time in micro-second resolution.
- // other getElapsedTime will call this first, then convert to correspond resolution.
- ///////////////////////////////////////////////////////////////////////////////
- double HighResolutionTimer::getElapsedTimeInMicroSec()
- {
- #ifdef WIN32
- if(!stopped)
- QueryPerformanceCounter(&endCount);
- startTimeInMicroSec = startCount.QuadPart * (1000000.0 / frequency.QuadPart);
- endTimeInMicroSec = endCount.QuadPart * (1000000.0 / frequency.QuadPart);
- #else
- if(!stopped)
- gettimeofday(&endCount, NULL);
- startTimeInMicroSec = (startCount.tv_sec * 1000000.0) + startCount.tv_usec;
- endTimeInMicroSec = (endCount.tv_sec * 1000000.0) + endCount.tv_usec;
- #endif
- return endTimeInMicroSec - startTimeInMicroSec;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // divide elapsedTimeInMicroSec by 1000
- ///////////////////////////////////////////////////////////////////////////////
- double HighResolutionTimer::getElapsedTimeInMilliSec()
- {
- return this->getElapsedTimeInMicroSec() * 0.001;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // divide elapsedTimeInMicroSec by 1000000
- ///////////////////////////////////////////////////////////////////////////////
- double HighResolutionTimer::getElapsedTimeInSec()
- {
- return this->getElapsedTimeInMicroSec() * 0.000001;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // same as getElapsedTimeInSec()
- ///////////////////////////////////////////////////////////////////////////////
- double HighResolutionTimer::getElapsedTime()
- {
- return this->getElapsedTimeInSec();
- }
|