Last active
August 29, 2015 14:20
-
-
Save sadmansk/55af501e2c5a7c349c4d to your computer and use it in GitHub Desktop.
A timer class encapsulating a static clock that uses the chrono library for nanoseconds precision. I made this for use with a game engine that I have been working on. Just wanted to share with people looking for this kind of precision when it comes to timers.
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
//since C++ does not allow definition of static data members inside header files, we need to use this .cpp file for initializing | |
//the starting time | |
#include "time.h" | |
std::chrono::time_point<clock_> Time::start = clock_::now(); |
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 <ctime> | |
#include <chrono> | |
#define SECOND 1000000000 | |
typedef std::chrono::high_resolution_clock clock_; | |
class Time | |
{ | |
public: | |
//ctor that is probably never gonna be used | |
inline Time() {} | |
//resets the starting time to the current time | |
inline static void reset() { start = clock_::now(); } | |
//returns the time elapsed from the starting time | |
inline static long long elapsed() { | |
return std::chrono::duration_cast<std::chrono::nanoseconds>(clock_::now() - start).count(); | |
} | |
inline virtual ~Time() {} | |
private: | |
static std::chrono::time_point<clock_> start; //variable to store the starting time | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment