Add a clock which gives deltas when polled

This commit is contained in:
Danny Robson 2013-08-05 16:40:43 +10:00
parent 6ace3c8b1d
commit 9ff59f8130
2 changed files with 31 additions and 3 deletions

View File

@ -27,8 +27,8 @@
using namespace util; using namespace util;
// ----------------------------------------------------------------------------
#ifdef PLATFORM_WIN32 #ifdef PLATFORM_WIN32
#include <windows.h> #include <windows.h>
uint64_t uint64_t
@ -48,7 +48,6 @@ util::sleep (uint64_t ns) {
} }
#else #else
#include <ctime> #include <ctime>
uint64_t uint64_t
@ -74,10 +73,24 @@ util::sleep (uint64_t ns) {
req = rem; req = rem;
} }
} }
#endif #endif
// ----------------------------------------------------------------------------
delta_clock::delta_clock():
time { util::nanoseconds (), util::nanoseconds () }
{ ; }
double
delta_clock::seconds (void) {
time.prev = time.curr;
time.curr = nanoseconds ();
return (time.curr - time.prev) / 1000000000.0;
}
// ----------------------------------------------------------------------------
util::polled_duration::polled_duration (std::string name, uint64_t interval): util::polled_duration::polled_duration (std::string name, uint64_t interval):
m_name (name), m_name (name),
m_interval (interval), m_interval (interval),

View File

@ -26,9 +26,24 @@
#include "stats.hpp" #include "stats.hpp"
namespace util { namespace util {
// ------------------------------------------------------------------------
uint64_t nanoseconds (void); uint64_t nanoseconds (void);
void sleep (uint64_t ns); void sleep (uint64_t ns);
// ------------------------------------------------------------------------
class delta_clock {
public:
delta_clock ();
double seconds (void);
protected:
struct {
uint64_t prev;
uint64_t curr;
} time;
};
// ------------------------------------------------------------------------
class polled_duration { class polled_duration {
public: public:
polled_duration (std::string name, uint64_t interval); polled_duration (std::string name, uint64_t interval);