-
Notifications
You must be signed in to change notification settings - Fork 7
/
Timer.cpp
57 lines (45 loc) · 1.58 KB
/
Timer.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
#include "stdafx.h"
#include "Timer.h"
#include <math.h>
Timer::Timer()
{
DWORD_PTR oldmask = ::SetThreadAffinityMask(::GetCurrentThread(), 0);
::QueryPerformanceCounter(&start_);
::SetThreadAffinityMask(::GetCurrentThread(), oldmask);
}
double Timer::GetFrequency()
{
LARGE_INTEGER proc_freq;
if(!::QueryPerformanceFrequency(&proc_freq))
{
return 1.0;
}
return static_cast<double>(proc_freq.QuadPart ? proc_freq.QuadPart : 1.0);
}
double Timer::GetElapsedTime()
{
LARGE_INTEGER stop;
DWORD_PTR oldmask = ::SetThreadAffinityMask(::GetCurrentThread(), 0);
::QueryPerformanceCounter(&stop);
static double frequency = GetFrequency();
::SetThreadAffinityMask(::GetCurrentThread(), oldmask);
return static_cast<double>(stop.QuadPart - start_.QuadPart) / frequency;
}
LONGLONG Timer::GetElapsedTime(LONGLONG resolutionPerSecond)
{
LARGE_INTEGER stop;
DWORD_PTR oldmask = ::SetThreadAffinityMask(::GetCurrentThread(), 0);
::QueryPerformanceCounter(&stop);
static double frequency = GetFrequency();
::SetThreadAffinityMask(::GetCurrentThread(), oldmask);
return static_cast<LONGLONG>(0.5 + floor(0.5 + static_cast<double>(stop.QuadPart - start_.QuadPart) * resolutionPerSecond / frequency));
}
std::wstring Timer::GetCurrentDateTime()
{
SYSTEMTIME systime;
GetLocalTime(&systime);
TCHAR szLogHeader[60] = { 0 };
_stprintf_s(szLogHeader, _countof( szLogHeader ), _T("[%02d:%02d:%02d.%03d] "),
systime.wHour, systime.wMinute, systime.wSecond, systime.wMilliseconds);
return std::wstring(szLogHeader);
}