Here is my little Hi-Res CPU timer class, I use it all the time in my apps to time various pieces of code, it can help to narrow bottlenecks in your code.
Usage :
Declare a variable of type CStopWatch..
eg. CStopWatch myTimer;
then at the start of the code you want to time, call :
myTimer.startTimer();
at the end of the code being timed, call :
myTimer.stopTimer();
to acces the value stored, which will need to be done once each time its called, or it will be overwritten, you could either display it straight to screen as a number, or you could store it in an array of some kind and write it out to a file to study. Either way, it amount of time passed from startTimer() to stopTimer() is accessed by calling :
double timePassed = myTimer.getElapsedTime();
The returned value is in seconds, accurate to microseconds. So will usually be quite below zero.
Lastly, the timer is completely seperate from DarkGDK, linking into windows directly and independantly of the GDK, and can be run and used in apps that dont include DarkGDK's header. The advantage is that the value being returned is the total time the code takes to execute. The advantage of using a timer other than GDK's dbTimer(); is that having not coded GDK's timer, we have no way of knowing exactly how it works, where it is called in each of the GDK's internal loops, how much work is done before or after its calls etc.
This code was originally written by David Bolton and was obtained from :
http://cplus.about.com/
There are a heap of resources for CPP on that site.
Header file "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();
};
Code file "hr_tim.cpp" :
//======================================================================================================
// hr_Time.cpp
// This is the implementation file for a CPU timer.... used to time your code im microseconds
//
// NOTE: see hr_Time.h for notes / usage
//======================================================================================================
#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) ;
}
If it ain't broke.... DONT FIX IT !!!