/ Published in: C++
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// How to use CStopWatch s; s.startTimer(); // put the code which you want to measure here // s.stopTimer(); double mtime = s.getElapsedTime() * 1000; // hr_time.h #include <windows.h> typedef struct { LARGE_INTEGER start; LARGE_INTEGER stop; } stopWatch; class CStopWatch { private: stopWatch timer; LARGE_INTEGER frequency; double LIToSecs( LARGE_INTEGER & L) ; public: CStopWatch() ; void startTimer( ) ; void stopTimer( ) ; double getElapsedTime() ; }; // hr_time.cpp #include <windows.h> #ifndef hr_timer #include "hr_time.h" #define hr_timer #endif double CStopWatch::LIToSecs( LARGE_INTEGER & L) { return ((double)L.QuadPart /(double)frequency.QuadPart) ; } CStopWatch::CStopWatch(){ timer.start.QuadPart=0; timer.stop.QuadPart=0; QueryPerformanceFrequency( &frequency ) ; } void CStopWatch::startTimer( ) { QueryPerformanceCounter(&timer.start) ; } void CStopWatch::stopTimer( ) { QueryPerformanceCounter(&timer.stop) ; } double CStopWatch::getElapsedTime() { LARGE_INTEGER time; time.QuadPart = timer.stop.QuadPart - timer.start.QuadPart; return LIToSecs( time) ; }
URL: http://cplus.about.com/od/howtodothingsi2/a/timing.htm