Skip to content

Instantly share code, notes, and snippets.

@Wunkolo
Created December 20, 2012 03:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Wunkolo/4342843 to your computer and use it in GitHub Desktop.
Save Wunkolo/4342843 to your computer and use it in GitHub Desktop.
Clock and Time
#include "Clock.h"
//platform specific time implementations.
#if defined(__WIN32) || defined(_WIN32)
//windows
#include <windows.h>
namespace
{
LARGE_INTEGER getFrequency()
{
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
return frequency;
}
}
#elif defined(linux) | defined(__linux)
//linux
#elif defined(__APPLE__) || defined(MACOSX) || defined(macintosh) || defined(Macintosh)
//mac
#include <mach/mach_time.h>
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
//BSD
#else
//?????
#endif
Clock::Clock(void) : tStartTime(curTime())
{
}
Time Clock::Elapsed()
{
return Time(curTime()) - tStartTime;
}
Time Clock::Reset()
{
Time now(curTime());
Time elapsed = now - tStartTime;
tStartTime = now;
return elapsed;
}
double Clock::curTime()
{
//Return current time in seconds using double accuracy.
//Per-platform implementation should be handled here.
//platform specific time implementations.
#if defined(__WIN32) || defined(_WIN32)
//windows
// Force the following code to run on first core
// (see http://msdn.microsoft.com/en-us/library/windows/desktop/ms644904(v=vs.85).aspx)
HANDLE currentThread = GetCurrentThread();
DWORD_PTR previousMask = SetThreadAffinityMask(currentThread, 1);
// Get the frequency of the performance counter
// (it is constant across the program lifetime)
static LARGE_INTEGER frequency = getFrequency();
// Get the current time
LARGE_INTEGER time;
QueryPerformanceCounter(&time);
// Restore the thread affinity
SetThreadAffinityMask(currentThread, previousMask);
// Return the current time as microseconds
return time.QuadPart / (float)frequency.QuadPart;
#elif defined(linux) | defined(__linux)
//linux
#elif defined(__APPLE__) || defined(MACOSX) || defined(macintosh) || defined(Macintosh)
//mac
static mach_timebase_info_data_t frequency = {0, 0};
if (frequency.denom == 0)
mach_timebase_info(&frequency);
Uint64 nanoseconds = mach_absolute_time() * frequency.numer / frequency.denom;
return nanoseconds*1000000000.0;//To seconds
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
//BSD
#else
//?????
return 0.0;//Unknown system.
#endif
}
Clock::~Clock(void)
{
}
#pragma once
#include "Time.h"
class Clock
{
public:
Clock(void);
Time Elapsed();
Time Reset();
~Clock(void);
private:
double curTime();
Time tStartTime;
};
#include "Time.h"
const Time Time::Zero;
Time::Time()
{
microseconds = 0;
}
Time::Time(long long MicroSeconds)
{
microseconds = MicroSeconds;
}
Time::Time(int MilliSeconds)
{
microseconds = MilliSeconds*1000;
}
Time::Time(double Seconds)
{
microseconds = long long(Seconds*1000000);
}
float Time::asSeconds() const
{
return microseconds/1000000.f;
}
int Time::asMilliseconds() const
{
return static_cast<int>(microseconds/1000);
}
long long Time::asMicroSeconds() const
{
return microseconds;
}
Time::~Time()
{
}
Time seconds(float amount)
{
return Time(static_cast<long long>(amount * 1000000));
}
Time milliseconds(int amount)
{
return Time(static_cast<long long>(amount) * 1000);
}
Time microseconds(long long amount)
{
return Time(amount);
}
bool operator ==(Time left, Time right)
{
return left.asMicroSeconds() == right.asMicroSeconds();
}
bool operator !=(Time left, Time right)
{
return left.asMicroSeconds() != right.asMicroSeconds();
}
bool operator <(Time left, Time right)
{
return left.asMicroSeconds() < right.asMicroSeconds();
}
bool operator >(Time left, Time right)
{
return left.asMicroSeconds() > right.asMicroSeconds();
}
bool operator <=(Time left, Time right)
{
return left.asMicroSeconds() <= right.asMicroSeconds();
}
bool operator >=(Time left, Time right)
{
return left.asMicroSeconds() >= right.asMicroSeconds();
}
Time operator -(Time right)
{
return microseconds(-right.asMicroSeconds());
}
Time operator +(Time left, Time right)
{
return microseconds(left.asMicroSeconds() + right.asMicroSeconds());
}
Time& operator +=(Time& left, Time right)
{
return left = left + right;
}
Time operator -(Time left, Time right)
{
return microseconds(left.asMicroSeconds() - right.asMicroSeconds());
}
Time& operator -=(Time& left, Time right)
{
return left = left - right;
}
Time operator *(Time left, float right)
{
return seconds(left.asSeconds() * right);
}
Time operator *(Time left, long long right)
{
return microseconds(left.asMicroSeconds() * right);
}
Time operator *(float left, Time right)
{
return right * left;
}
Time operator *(long long left, Time right)
{
return right * left;
}
Time& operator *=(Time& left, float right)
{
return left = left * right;
}
Time& operator *=(Time& left, long long right)
{
return left = left * right;
}
Time operator /(Time left, float right)
{
return seconds(left.asSeconds() / right);
}
Time operator /(Time left, long long right)
{
return microseconds(left.asMicroSeconds() / right);
}
Time& operator /=(Time& left, float right)
{
return left = left / right;
}
Time& operator /=(Time& left, long long right)
{
return left = left / right;
}
#pragma once
class Time
{
public:
Time();
Time(long long MicroSeconds);
Time(int MilliSeconds);
Time(double Seconds);
float asSeconds() const;
int asMilliseconds() const;
long long asMicroSeconds() const;
static const Time Zero;
virtual ~Time();
private:
long long microseconds;
friend Time seconds(float);
friend Time milliseconds(int);
friend Time microseconds(long long);
};
bool operator == (Time left, Time right);
bool operator != (Time left, Time right);
bool operator < (Time left, Time right);
bool operator > (Time left, Time right);
bool operator >= (Time left, Time right);
bool operator <= (Time left, Time right);
Time operator - (Time right);
Time operator + (Time left, Time right);
Time& operator += (Time& left, Time right);
Time operator - (Time left, Time right);
Time& operator -= (Time& left, Time right);
Time operator * (Time left, float right);
Time operator * (Time left, Time right);
Time& operator *= (Time left, float right);
Time& operator *= (Time& left, Time right);
Time operator / (Time left, float right);
Time operator / (Time left, Time right);
Time& operator /= (Time& left, Time right);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment