Created
May 11, 2014 12:00
-
-
Save serge1/e3f9bfe6f45593b4ecd4 to your computer and use it in GitHub Desktop.
High Precision Timer for Windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
class HPTimer | |
{ | |
public: | |
HPTimer( std::string name ) : total(0.0), name_(name) | |
{ | |
} | |
void reset() | |
{ | |
total = 0.0; | |
} | |
void start() | |
{ | |
QueryPerformanceCounter(&start_); | |
} | |
void stop() | |
{ | |
QueryPerformanceCounter( &end_ ); | |
QueryPerformanceFrequency( &frequency ); | |
total += (double)( end_.QuadPart - start_.QuadPart ) / frequency.QuadPart; | |
} | |
void show() | |
{ | |
char text[200]; | |
sprintf( text, "%f", get_total() ); | |
MessageBox( 0, text, name_.c_str(), MB_OK ); | |
} | |
double get_total() | |
{ | |
return total; | |
} | |
private: | |
LARGE_INTEGER frequency; | |
LARGE_INTEGER start_; | |
LARGE_INTEGER end_; | |
double total; | |
std::string name_; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func() | |
{ | |
// Your code is here | |
HPTimer timer( "Cumulative time" ); | |
timer.start(); | |
// Your code is here too | |
timer.stop(); | |
timer.start(); | |
// Your code is here too | |
timer.stop(); | |
// and here | |
timer.show(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment