-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimer.cpp
More file actions
66 lines (54 loc) · 1.35 KB
/
timer.cpp
File metadata and controls
66 lines (54 loc) · 1.35 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
/*
This file stems from a Fast Downward version dating around December 2014.
*/
#include "timer.h"
//#include "utilities.h"
#include <ostream>
#include <unistd.h>
#include <sys/times.h>
using namespace std;
Timer::Timer() {
last_start_clock = current_clock();
collected_time = 0;
stopped = false;
}
Timer::~Timer() {
}
double Timer::current_clock() const {
struct tms the_tms;
times(&the_tms);
clock_t clocks = the_tms.tms_utime + the_tms.tms_stime;
return double(clocks) / sysconf(_SC_CLK_TCK);
}
double Timer::stop() {
collected_time = (*this)();
stopped = true;
return collected_time;
}
double Timer::operator()() const {
if (stopped)
return collected_time;
else
return collected_time + current_clock() - last_start_clock;
}
void Timer::resume() {
if (stopped) {
stopped = false;
last_start_clock = current_clock();
}
}
double Timer::reset() {
double result = (*this)();
collected_time = 0;
last_start_clock = current_clock();
return result;
}
ostream &operator<<(ostream &os, const Timer &timer) {
double value = timer();
if (value < 0 && value > -1e-10)
value = 0.0; // We sometimes get inaccuracies from god knows where.
if (value < 1e-10)
value = 0.0; // Don't care about such small values.
os << value << "s";
return os;
}