https://github.com/Microsoft/CNTK
Revision b8e1fc8241dbf07680b41f3f954c465369fca2c2 authored by Mark Hillebrand on 18 January 2016, 08:32:07 UTC, committed by Mark Hillebrand on 18 January 2016, 08:32:07 UTC
1 parent 4a484bf
Raw File
Tip revision: b8e1fc8241dbf07680b41f3f954c465369fca2c2 authored by Mark Hillebrand on 18 January 2016, 08:32:07 UTC
License change
Tip revision: b8e1fc8
TimerUtility.cpp
#include "TimerUtility.h"
#include <assert.h>
#ifdef WIN32
#include <Windows.h>
static LARGE_INTEGER s_ticksPerSecond;
static BOOL s_setFreq = QueryPerformanceFrequency(&s_ticksPerSecond);
#else
#include <time.h>
#endif

namespace Microsoft { namespace MSR { namespace CNTK {
    long long Timer::GetStamp()
    {
#ifdef WIN32
        LARGE_INTEGER li;
        QueryPerformanceCounter(&li);
        return li.QuadPart;
#else
        timespec ts;
        clock_gettime(CLOCK_REALTIME, &ts); // Works on Linux

        long long ret = ts.tv_sec * NANO_PER_SEC + ts.tv_nsec;

        return ret;
#endif
    }

    void Timer::Start()
    {
        m_start = GetStamp();
    }

    void Timer::Restart()
    {
        m_start = m_end = 0;
        Start();
    }

    void Timer::Stop()
    {
        m_end = GetStamp();
    }

    long long Timer::ElapsedMicroseconds()
    {
        assert(m_start != 0);
        long long diff = 0;

        if (m_end != 0)
        {
            diff = m_end - m_start;
        }
        else
        {
            diff = GetStamp() - m_start;
        }

        if (diff < 0)
        {
            diff = 0;
        }

#ifdef WIN32
        assert(s_setFreq == TRUE);
        return (diff * MICRO_PER_SEC) / s_ticksPerSecond.QuadPart;
#else
        return diff / MICRO_PER_NANO;
#endif
    }

}}}
back to top