-
Notifications
You must be signed in to change notification settings - Fork 0
/
timers.cpp
104 lines (71 loc) · 2.59 KB
/
timers.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
104
#include "timers.h"
// Constructor for Particles
Timers::Timers() {
// By default there are 2 timers :
// - initilization
// - main loop
names.resize(2);
temporary_times.resize(2);
accumulated_times.resize(2);
names[0] = "initialization";
names[1] = "main loop";
temporary_times[0] = 0;
temporary_times[1] = 0;
accumulated_times[0] = 0;
accumulated_times[1] = 0;
};
// Destructor
Timers::~Timers() {
};
// Add a timer to the internal list
void Timers::add(std::string name) {
names.push_back(name);
temporary_times.push_back(0);
accumulated_times.push_back(0);
}
// Start the specified timer
void Timers::start(std::string name) {
int id = index(name);
struct timeval time;
gettimeofday(&time, NULL);
temporary_times[id] = time.tv_sec + time.tv_usec*1e-6;
}
// Stop the specified timer
void Timers::stop(std::string name) {
int id = index(name);
struct timeval time;
gettimeofday(&time, NULL);
accumulated_times[id] += (time.tv_sec + time.tv_usec*1e-6) - temporary_times[id];
}
// Return the specicied timer index
int Timers::index(std::string name) {
int index = 0;
while(names[index] != name && index < names.size()) {
index++;
}
if (index >= names.size()) {
index = -1;
}
return index;
}
void Timers::print() {
double percentage;
std::cout << " ------------------------------------ "<< std::endl;
std::cout << " TIMERS"<< std::endl;
std::cout << " ------------------------------------ "<< std::endl;
std::cout << " code part | time (s) | percentage |"<< std::endl;
std::cout << " ---------------------|------------|----------- |"<< std::endl;
std::cout << " " << std::setw(20) << names[0] ;
std::cout << " | " << std::fixed << std::setprecision(6) << std::setw(10) << accumulated_times[0] ;
std::cout << " | - %";
std::cout << " | " ;
std::cout << std::endl;
for (int i = 1 ; i < names.size() ; i++) {
percentage = accumulated_times[i] / (accumulated_times[1] ) * 100;
std::cout << " " << std::setw(20) << names[i] ;
std::cout << " | " << std::fixed << std::setprecision(6) << std::setw(10) << accumulated_times[i];
std::cout << " | " << std::fixed << std::setprecision(2) << std::setw(8) << percentage << " %";
std::cout << " | " ;
std::cout << std::endl;
}
}