yipo
8/14/2015 - 8:38 AM

High Resolution Clock

High Resolution Clock

#include "highres_clock.hpp"
#include <assert.h>
#include <math.h>

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

void test_case()
{
    const unsigned int INTERVAL = 200;

    highres_clock clock;
    Sleep(INTERVAL);
    assert(abs(clock.elapse() - static_cast<double>(INTERVAL)) < 1.0);

    clock.reset();
    assert(clock.elapse() < 1.0);
}

int main()
{
    test_case();
    return 0;
}

#pragma once

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

class highres_clock
{
    LARGE_INTEGER m_begin;

public:
    highres_clock()
    {
        reset();
    }

    void reset()
    {
        QueryPerformanceCounter(&m_begin);
    }

    double elapse() const
    {
        LARGE_INTEGER now, freq;
        QueryPerformanceCounter(&now);
        QueryPerformanceFrequency(&freq);
        return 1000.0 * static_cast<double>(now.QuadPart - m_begin.QuadPart) / static_cast<double>(freq.QuadPart);
    }
};


project(highres_clock)
add_executable(${PROJECT_NAME} highres_clock.hpp highres_clock_test.cpp)
@ECHO OFF
CD /D %~dp0

PATH ^
%ProgramFiles%\CMake\bin;^
%ProgramFiles(x86)%\CMake\bin

IF NOT EXIST build MKDIR build
CD build

cmake %* ..
PAUSE