-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicTimer.h
More file actions
70 lines (58 loc) · 1.62 KB
/
BasicTimer.h
File metadata and controls
70 lines (58 loc) · 1.62 KB
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
#ifndef BASICTIMER_H
#define BASICTIMER_H
//#include <cstdio>
#include <ctime>
#include <sstream>
#include <string>
class BasicTimer
{
public:
BasicTimer() : TimerCount(0) {}
void Start() { clock_gettime(CLOCK_REALTIME, &StartTime); }
void Stop() { clock_gettime(CLOCK_REALTIME, &StopTime); TimerCount++; }
float GetAverageTimeMs()
{
timespec TimeDiff = GetTimeDiff(StartTime, StopTime);
return TimespecToMs(TimeDiff);
//return 1.f;
//printf("TimerCount = %llu\n", TimerCount);
}
float GetTimeSinceLastCheck()
{
clock_gettime(CLOCK_REALTIME, &StopTime);
float time = TimespecToSec(GetTimeDiff(StartTime, StopTime));
StartTime = StopTime;
return time;
}
std::string GetAverageTimeMsStr()
{
std::ostringstream ss;
ss << GetAverageTimeMs();
return std::string(ss.str());
}
unsigned long long int TimerCount;
private:
timespec StartTime;
timespec StopTime;
timespec GetTimeDiff(timespec start, timespec end)
{
timespec temp;
if ((end.tv_nsec-start.tv_nsec)<0) {
temp.tv_sec = end.tv_sec-start.tv_sec-1;
temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
} else {
temp.tv_sec = end.tv_sec-start.tv_sec;
temp.tv_nsec = end.tv_nsec-start.tv_nsec;
}
return temp;
}
float TimespecToMs(timespec time)
{
return (time.tv_sec * 1000 + time.tv_nsec / 1000000.f);
}
float TimespecToSec(timespec time)
{
return TimespecToMs(time) / 1000.f;
}
};
#endif // BASICTIMER_H