-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTimer.h
100 lines (84 loc) · 2.25 KB
/
Timer.h
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
94
95
96
97
98
99
100
/*
Copyright (c) 2017, The University of Bristol, Senate House, Tyndall Avenue, Bristol, BS8 1TH, United Kingdom.
Copyright (c) 2021, COSIC-KU Leuven, Kasteelpark Arenberg 10, bus 2452, B-3001 Leuven-Heverlee, Belgium.
All rights reserved
*/
#ifndef _timer
#define _timer
#include <sys/time.h>
#include <sys/wait.h> /* Wait for Process Termination */
#include <time.h>
#include <stdexcept>
class Timer_Error : public std::runtime_error {
public:
Timer_Error(const char* what) : std::runtime_error(what) {}
};
;
long long timeval_diff(struct timeval *start_time, struct timeval *end_time);
double timeval_diff_in_seconds(struct timeval *start_time, struct timeval *end_time);
// no clock_gettime() on OS X
#ifdef __MACH__
#define timespec timeval
#define clockid_t int
#define CLOCK_MONOTONIC 0
#define CLOCK_PROCESS_CPUTIME_ID 0
#define CLOCK_THREAD_CPUTIME_ID 0
#define timespec_diff timeval_diff
#define clock_gettime(x, y) gettimeofday(y, 0)
#else
long long timespec_diff(struct timespec *start_time, struct timespec *end_time);
#endif
#define BENCH_TEXT_BOLD "\x1B[1m"
#define BENCH_COLOR_RED "\x1B[31m"
#define BENCH_COLOR_GREEN "\x1B[32m"
#define BENCH_COLOR_BLUE "\x1B[34m"
#define BENCH_ATTR_RESET "\x1B[0m"
#define BENCH_MAGIC_START "<b3m4>\n"
#define BENCH_MAGIC_END "</b3m4>\n"
class Timer
{
timespec startv;
bool running;
long long elapsed_time;
clockid_t clock_id;
public:
long long elapsed_since_last_start()
{
timespec endv;
clock_gettime(clock_id, &endv);
return timespec_diff(&startv, &endv);
}
Timer(clockid_t clock_id= CLOCK_MONOTONIC)
: running(false), elapsed_time(0), clock_id(clock_id)
{
clock_gettime(clock_id, &startv);
}
Timer &start()
{
if (running)
{
throw Timer_Error("Timer already running.");
}
// clock() is not suitable in threaded programs so time using something else
clock_gettime(clock_id, &startv);
running= true;
return *this;
}
void stop()
{
if (!running)
{
throw Timer_Error("Time not running.");
}
elapsed_time+= elapsed_since_last_start();
running= false;
clock_gettime(clock_id, &startv);
}
void reset()
{
elapsed_time= 0;
clock_gettime(clock_id, &startv);
}
double elapsed();
};
#endif