-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimer.cpp
64 lines (54 loc) · 1.47 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
58
59
60
61
62
63
64
#include "Timer.h"
#ifdef _WIN32
void Timer::start()
{
//Frequenz holen
if (!QueryPerformanceFrequency((LARGE_INTEGER*)&g_Frequency))
std::cout << "Performance Counter nicht vorhanden" << std::endl;
//1. Messung
QueryPerformanceCounter((LARGE_INTEGER*)&g_CurentCount);
}
void Timer::stop()
{
//2. Messung
QueryPerformanceCounter((LARGE_INTEGER*)&g_LastCount);
}
double Timer::getTime()
{
QueryPerformanceCounter((LARGE_INTEGER*)&g_LastCount);
double dTimeDiff = (((double)(g_LastCount - g_CurentCount)) / ((double) g_Frequency));
dTimeDiff *= 1000;
return dTimeDiff;
}
std::ostream & operator<<(std::ostream& os, const Timer &t)
{
double dTimeDiff = (((double)(t.g_LastCount - t.g_CurentCount)) / ((double)t.g_Frequency));
dTimeDiff *= 1000;
std::string ext = "ms";
os << dTimeDiff << " " << ext;
return os;
}
#else
void Timer::start() {
id = CLOCK_MONOTONIC;
clock_gettime(id, &ts_start);
}
void Timer::stop() {
clock_gettime(id, &ts_end);
}
double Timer::getTime(){
clock_gettime(id, &ts_end);
unsigned long time = (ts_end.tv_sec - ts_start.tv_sec) * 1000000000 + ts_end.tv_nsec - ts_start.tv_nsec;
time /= 1000; //us
time /= 1000; //ms
return (double) time;
}
std::ostream & operator<<(std::ostream& os, const Timer &t)
{
unsigned long ms = (t.ts_end.tv_sec - t.ts_start.tv_sec) * 1000000000 + t.ts_end.tv_nsec - t.ts_start.tv_nsec;
ms /= 1000;
ms /= 1000;
os << ms << " ms";
return os;
}
#endif