forked from bristolcrypto/SPDZ-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime-func.h
92 lines (74 loc) · 1.93 KB
/
time-func.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
// (C) 2017 University of Bristol. See License.txt
#ifndef _timer
#define _timer
#include <sys/wait.h> /* Wait for Process Termination */
#include <sys/time.h>
#include <time.h>
#include "Exceptions/Exceptions.h"
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
class Timer
{
public:
Timer(clockid_t clock_id = CLOCK_MONOTONIC) : running(false), elapsed_time(0), clock_id(clock_id)
{ clock_gettime(clock_id, &startv); }
Timer& start();
void stop();
void reset();
double elapsed();
double idle();
private:
timespec startv;
bool running;
long long elapsed_time;
clockid_t clock_id;
long long elapsed_since_last_start();
};
class TimeScope
{
Timer& timer;
public:
TimeScope(Timer& timer) : timer(timer) { timer.start(); }
~TimeScope() { timer.stop(); }
};
inline Timer& Timer::start()
{
if (running)
throw Processor_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;
}
inline void Timer::stop()
{
if (!running)
throw Processor_Error("Time not running.");
elapsed_time += elapsed_since_last_start();
running = false;
clock_gettime(clock_id, &startv);
}
inline void Timer::reset()
{
elapsed_time = 0;
clock_gettime(clock_id, &startv);
}
inline long long Timer::elapsed_since_last_start()
{
timespec endv;
clock_gettime(clock_id, &endv);
return timespec_diff(&startv, &endv);
}
#endif