-
Notifications
You must be signed in to change notification settings - Fork 1
/
ElapsedTimer.cpp
56 lines (43 loc) · 1.28 KB
/
ElapsedTimer.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
#include "ElapsedTimer.hpp"
namespace cornus {
ElapsedTimer::ElapsedTimer() {}
ElapsedTimer::~ElapsedTimer() {}
inline i64 GetDiffMs(const timespec &end, const timespec &start) {
return (end.tv_sec - start.tv_sec) * 1000L + (end.tv_nsec - start.tv_nsec) / 1000000L;
}
inline i64 GetDiffMc(const timespec &end, const timespec &start) {
return (end.tv_sec - start.tv_sec) * 1000000L + (end.tv_nsec - start.tv_nsec) / 1000L;
}
inline i64 GetDiffNano(const timespec &end, const timespec &start) {
return (end.tv_sec - start.tv_sec) * 1000000000L + (end.tv_nsec - start.tv_nsec);
}
void ElapsedTimer::Continue(const Reset r)
{
if (r == Reset::Yes)
worked_time_nano_ = 0;
cint status = clock_gettime(clock_type_, &start_);
if (status != 0)
mtl_status(errno);
}
i64 ElapsedTimer::elapsed_nano()
{
cbool paused = (start_.tv_sec == 0 && start_.tv_nsec == 0);
if (paused)
return worked_time_nano_;
struct timespec now;
int status = clock_gettime(clock_type_, &now);
if (status != 0)
mtl_status(errno);
const i64 extra = GetDiffNano(now, start_);
return worked_time_nano_ + extra;
}
void ElapsedTimer::Pause()
{
struct timespec now;
cint status = clock_gettime(clock_type_, &now);
if (status != 0)
mtl_status(errno);
worked_time_nano_ += GetDiffNano(now, start_);
start_ = {};
}
}