timer.cpp 1.69 KB
Newer Older
qon's avatar
qon committed
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "timer.h"
#ifdef _WIN32
#include <windows.h>
#include <winbase.h>
#else
#include <time.h>
#endif

HighResTimer::HighResTimer()
{
	ElapsedTime = 0;
	running = 0;
}

HighResTimer::~HighResTimer() {}

void HighResTimer::Start()
{
#ifdef _WIN32
	__int64 istart;
	QueryPerformanceCounter((LARGE_INTEGER*)&istart);
	StartTime = (double) istart;
#else
	timespec tv;
	clock_gettime(CLOCK_REALTIME, &tv);
	StartTime = (double) tv.tv_sec * 1.0E9 + (double) tv.tv_nsec;
#endif
	running = 1;
}

void HighResTimer::ResetStart()
{
	ElapsedTime = 0;
	Start();
}

void HighResTimer::Stop()
{
	if (running == 0) return;
	running = 0;
	double EndTime = 0;
#ifdef _WIN32
	__int64 iend;
	QueryPerformanceCounter((LARGE_INTEGER*) &iend);
	EndTime = (double) iend;
#else
	timespec tv;
	clock_gettime(CLOCK_REALTIME, &tv);
	EndTime = (double) tv.tv_sec * 1.0E9 + (double) tv.tv_nsec;
#endif
	ElapsedTime += EndTime - StartTime;
}

void HighResTimer::Reset()
{
	ElapsedTime = 0;
	StartTime = 0;
	running = 0;
}

double HighResTimer::GetElapsedTime()
{
	return ElapsedTime / Frequency;
}

double HighResTimer::GetCurrentElapsedTime()
{
	if (running == 0) return(GetElapsedTime());
	double CurrentTime = 0;
#ifdef _WIN32
	__int64 iend;
	QueryPerformanceCounter((LARGE_INTEGER*) &iend);
	CurrentTime = (double) iend;
#else
	timespec tv;
	clock_gettime(CLOCK_REALTIME, &tv);
	CurrentTime = (double) tv.tv_sec * 1.0E9 + (double) tv.tv_nsec;
#endif
	return((CurrentTime - StartTime + ElapsedTime) / Frequency);
}

double HighResTimer::GetFrequency()
{
#ifdef _WIN32
	__int64 ifreq;
	QueryPerformanceFrequency((LARGE_INTEGER*)&ifreq);
	return((double) ifreq);
#else
	return(1.0E9);
#endif
}

double HighResTimer::Frequency = HighResTimer::GetFrequency();