forked from microsoft/CNTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimerUtility.cpp
72 lines (61 loc) · 1.15 KB
/
TimerUtility.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "TimerUtility.h"
#include <assert.h>
#ifdef WIN32
#define NOMINMAX
#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
}
} } }