Skip to content

Instantly share code, notes, and snippets.

@serge1
Created May 11, 2014 12:00
Show Gist options
  • Save serge1/e3f9bfe6f45593b4ecd4 to your computer and use it in GitHub Desktop.
Save serge1/e3f9bfe6f45593b4ecd4 to your computer and use it in GitHub Desktop.
High Precision Timer for Windows
#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_;
};
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