-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.cpp
161 lines (123 loc) · 4.88 KB
/
timer.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright 2016 Vegard Antun
//
#include "timer.h"
/*
timeit is a function specially designed to test the computational time of
various transforms.
timeit test the computational time of the function `transform` running it
`runs` number of times. The timeit function will output the computational
statistics from these runs to stdout. The `name` will identify the name of the
function being called. The length of the input vector to the transform
function will be `N`.
INPUT:
name - String identifying which functions is being called.
runs - Number of times the `transform` functions should be called.
N - Length of the vector the `transform` function should transform.
transform - The transform function. The first argument to this function will be
a random vector of doubles it should transform, the second argument
is the length of the transform.
*/
void timeit( const char * name, const int runs, const unsigned long N,
void (*transform)(double*, const unsigned long))
{
// Create vector with random numbers
std::vector<double> s(N);
std::mt19937 gen(0);
std::uniform_real_distribution<double> dis(0, 1);
std::generate(s.begin(), s.end(), [ &dis, &gen ] () { return dis(gen); });
// Prepare for time measurements
std::vector<double> time_measurements;
std::chrono::time_point<std::chrono::high_resolution_clock>
start_time_local, end_time_local;
for (int i = 0; i < runs; i++) {
start_time_local = std::chrono::high_resolution_clock::now();
transform( &s[0], N );
end_time_local = std::chrono::high_resolution_clock::now();
time_measurements.push_back(std::chrono::duration_cast<
std::chrono::duration<double> >
(end_time_local - start_time_local).count());
}
double min=0, mean=0, median=0, sd=0;
std::tie(min,mean, median, sd) = statistics(time_measurements);
int spaceing = 8;
std::cout << std::left << std::setw(10) << name
<< " Min: " << std::left << std::setw(spaceing) << min
<< " Mean: " << std::left << std::setw(spaceing) << mean
<< " Median: " << std::left << std::setw(spaceing) << median
<< " sd: " << std::left << std::setw(spaceing) << sd << std::endl;
}
/*
statistics computes the computational statistics found in the vector `val`. The
following entries are being returned as 4-tuple:
min value, mean, median and standard deviation.
*/
std::tuple<double, double, double, double> statistics(const std::vector<double> &val)
{
size_t N = val.size();
double mean = 0, sd = 0, median = 0, min = 0;
for(double v : val) mean += v / N;
for(double v : val) sd += (v - mean)*(v - mean);
sd = std::sqrt(sd/N);
std::vector<double> val_sorted(val);
std::sort(val_sorted.begin(), val_sorted.end());
min = val_sorted[0];
median = (N%2) ? val_sorted[N/2+1] : (val_sorted[N/2-1] + val_sorted[N/2-1])/2;
return std::make_tuple(min, mean, median, sd);
}
/*
Start the time measurement.
*/
void Timer::start() {
if (is_active) {
std::cerr << "Using Timer::start() wrong" << std::endl;
} else {
start_time = std::chrono::high_resolution_clock::now();
is_active = true;
}
}
/*
Pauses the time measurement.
*/
void Timer::pause() {
if (is_active) {
end_time = std::chrono::high_resolution_clock::now();
elapsed_time += end_time - start_time;
is_active = false;
} else {
std::cerr << "Using Timer::end() wrong" << std::endl;
}
}
/*
Sets the name of the timer. It is advisable to set the name of the timer before
one call the stop() function, to get a more intuitive output.
*/
void Timer::set_name(const char * new_name) {
std::strcpy(name, new_name);
}
/*
Stop the time measurement. The function will write the elapsed time to stdout.
*/
void Timer::stop() {
if (is_active) {
pause();
}
if (name[0] == '\0') {
std::cout << "Elapsed time: " << elapsed_time.count() << " s"
<< std::endl;
} else {
std::cout << "Time used by " << name << ": " << elapsed_time.count()
<< " s" << std::endl;
}
}