forked from data61/MP-SPDZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime-func.cpp
103 lines (86 loc) · 2.16 KB
/
time-func.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
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
101
102
103
#include "Tools/time-func.h"
#include "Tools/Exceptions.h"
#include <assert.h>
long long timeval_diff(struct timeval *start_time, struct timeval *end_time)
{ struct timeval temp_diff;
struct timeval *difference;
difference=&temp_diff;
difference->tv_sec =end_time->tv_sec -start_time->tv_sec ;
difference->tv_usec=end_time->tv_usec-start_time->tv_usec;
while(difference->tv_usec<0)
{ difference->tv_usec+=1000000;
difference->tv_sec -=1;
}
return 1000000LL*difference->tv_sec+difference->tv_usec;
}
double timeval_diff_in_seconds(struct timeval *start_time, struct timeval *end_time)
{
return double(timeval_diff(start_time, end_time)) / 1e6;
}
long long timespec_diff(const struct timespec *start_time, const struct timespec *end_time)
{
long long sec =end_time->tv_sec -start_time->tv_sec ;
long long nsec=end_time->tv_nsec-start_time->tv_nsec;
while(nsec<0)
{ nsec+=1000000000;
sec -=1;
}
return 1000000000*sec+nsec;
}
double convert_ns_to_seconds(long long x)
{
return double(x) / 1e9;
}
double Timer::elapsed()
{
long long res = elapsed_time;
if (running)
res += elapsed_since_last_start();
return convert_ns_to_seconds(res);
}
double Timer::elapsed_then_reset()
{
double res = elapsed();
reset();
return res;
}
double Timer::idle()
{
if (running)
throw Processor_Error("Timer running.");
else
return convert_ns_to_seconds(elapsed_since_last_start());
}
bool Timer::is_running()
{
return running;
}
Timer& Timer::operator -=(const Timer& other)
{
assert(clock_id == other.clock_id);
assert(not running);
assert(not other.running);
elapsed_time -= other.elapsed_time;
return *this;
}
Timer& Timer::operator +=(const Timer& other)
{
assert(clock_id == other.clock_id);
assert(not running);
elapsed_time += other.elapsed_time
+ other.running * other.elapsed_since_last_start();
return *this;
}
Timer& Timer::operator +=(const TimeScope& other)
{
assert(clock_id == other.timer.clock_id);
assert(not running);
elapsed_time += other.timer.elapsed_since_last_start();
return *this;
}
Timer Timer::operator +(const Timer& other) const
{
Timer res = *this;
res += other;
return res;
}