-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathget_time.h
85 lines (71 loc) · 1.63 KB
/
get_time.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
#pragma once
#include <stdlib.h>
#include <sys/time.h>
#include <iomanip>
#include <iostream>
#include <string>
struct timer {
double total_time;
double last_time;
bool on;
std::string name;
struct timezone tzp;
timer(std::string name = "PBBS time", bool _start = true)
: total_time(0.0), on(false), name(name), tzp({0,0}) {
if (_start) start();
}
double get_time() {
timeval now;
gettimeofday(&now, &tzp);
return ((double) now.tv_sec) + ((double) now.tv_usec)/1000000.;
}
void start () {
on = 1;
last_time = get_time();
}
double stop () {
on = 0;
double d = (get_time()-last_time);
total_time += d;
return d;
}
void reset() {
total_time=0.0;
on=0;
}
double get_total() {
if (on) return total_time + get_time() - last_time;
else return total_time;
}
double get_next() {
if (!on) return 0.0;
double t = get_time();
double td = t - last_time;
total_time += td;
last_time = t;
return td;
}
void report(double time, std::string str) {
std::ios::fmtflags cout_settings = std::cout.flags();
std::cout.precision(4);
std::cout << std::fixed;
std::cout << name << ": ";
if (str.length() > 0)
std::cout << str << ": ";
std::cout << time << std::endl;
std::cout.flags(cout_settings);
}
void total() {
report(get_total(),"total");
total_time = 0.0;
}
void reportTotal(std::string str) {
report(get_total(), str);
}
void next(std::string str) {
if (on) report(get_next(), str);
}
};
static timer _tm;
#define startTime() _tm.start();
#define nextTime(_string) _tm.next(_string);