-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperiment.hpp
266 lines (215 loc) · 6.16 KB
/
experiment.hpp
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// experiment.hpp
/*
* proggen_searchtexts
* Copyright (C) 2011 Alexander Korsunsky
*
* 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, either 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/>.
*/
#ifndef EXPERIMENT_HPP_INCLUDED_
#define EXPERIMENT_HPP_INCLUDED_
// C-Library
#include <cstring>
#include <cmath>
// C++-Library
#include <iostream>
#include <utility>
#include <vector>
#include <valarray>
#include <map>
#include <memory>
#include <numeric>
// POSIX-Library
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/time.h>
// for use with std::shared_ptr
struct MMapDeleter
{
const std::size_t _filesize;
MMapDeleter(std::size_t filesize)
: _filesize(filesize)
{}
void operator () (void* mapping) const
{
if (mapping != nullptr && mapping != MAP_FAILED)
{
munmap(mapping, _filesize);
}
}
};
// for use with std::shared_ptr
template<typename T>
struct ArrayDeleter
{
void operator () (T* d) const
{
delete [] d;
}
};
std::shared_ptr<char> readFile(char const *filename);
inline void nicedisp(std::ostream& os, double value)
{
if (value < 1e3)
os<<value<<" microseconds";
else if(value < 1e6)
os<<value/1e3<<" milliseconds";
else
os<<value/1e6<<" seconds";
}
struct ExperimentStatistics
{
ExperimentStatistics(const std::vector<double>& data, const std::string& name_ = std::string())
: name(name_)
{
N = data.size();
mean = std::accumulate(data.begin(), data.end(), 0.0);
mean /= N;
std_dev = sqrt(
std::accumulate(data.begin(), data.end(), 0.0,
[this](double sum, double val)
{return sum +(mean - val)*(mean - val);}
)
);
}
ExperimentStatistics(const ExperimentStatistics& other) = default;
void display()
{
std::cout.precision(3);
std::cout<<"Experiment"<<name<<" with N = "<<N<<":\n"<<"\tAverage: ";
nicedisp(std::cout, mean);
std::cout<<".\n";
std::cout<<"\tStandard deviation: ";
nicedisp(std::cout, std_dev);
std::cout<<".\n";
std::cout<<"\tRelat. standard deviation: "<<std_dev/mean*100<<"%\n";
}
std::string name;
std::size_t N;
double mean;
double std_dev;
};
// This is the "ugly" but parsable version of displaying the statistical data
inline
std::ostream& operator << (std::ostream& os, const ExperimentStatistics& stats)
{
return os<<
"N:"<<stats.N<<';'<<
"Mean:"<<stats.mean<<';'<<
"StdDev:"<<stats.std_dev;
}
template <typename Candidate>
class Experiment
{
public:
typedef std::pair<
const char* /*text id*/,
const char* /*text file name*/
> input_t;
typedef std::pair<
const char* /*text id*/,
std::shared_ptr<char> /*text file data*/
> text_t;
typedef std::map<
const char*, // file name
std::vector<const char*> // search patterns
> searches_t;
Experiment(std::vector<input_t> input, searches_t searches);
void conductExperiment(std::size_t N);
std::vector<double> _durations_init;
std::vector<double> _durations_search;
private:
std::vector<text_t> _input_data;
searches_t _searches;
};
template <typename Candidate>
Experiment<Candidate>::Experiment(
std::vector<input_t> input, searches_t searches)
: _searches(searches)
{
// load files that were possible to open
for (auto& i: input)
{
auto filedata = readFile(i.second);
if (filedata) _input_data.push_back(text_t{i.first, filedata});
}
}
template <typename Candidate>
void Experiment<Candidate>::conductExperiment(std::size_t N)
{
Candidate* candidate = nullptr;
struct timeval start_tv, interm_tv, end_tv;
for (std::size_t current_run = 0; current_run < N; ++current_run)
{
candidate = new Candidate;
//Zeitmessung startet
gettimeofday(&start_tv, 0);
// add texts
for (auto& text : _input_data)
{
candidate->addText(text.first, text.second.get());
}
gettimeofday(&interm_tv, 0);
// perform search operations
for (auto& outfile : _searches)
{
for (const char* pattern : outfile.second)
candidate->addPattern(pattern);
// GO!
candidate->seek(outfile.first);
}
// Zeitmessung endet
gettimeofday(&end_tv, 0);
// push back microseconds
_durations_init.push_back(
(interm_tv.tv_sec - start_tv.tv_sec) * 1e6 +
(interm_tv.tv_usec - start_tv.tv_usec)
);
_durations_search.push_back(
(end_tv.tv_sec - interm_tv.tv_sec) * 1e6 +
(end_tv.tv_usec - interm_tv.tv_usec)
);
//Destruktor läuft
delete candidate;
}
}
std::shared_ptr<char> readFile(char const *filename)
{
off_t filesize = 0;
int fd = open(filename, O_RDONLY);
if (fd == -1)
{
perror("Failed to open file for reading");
return nullptr;
}
filesize = lseek(fd, 0, SEEK_END);
std::shared_ptr<void> filebuf(
mmap(0, filesize, PROT_READ, MAP_SHARED, fd, 0), MMapDeleter(filesize));
if (filebuf.get() == MAP_FAILED)
{
perror("Failed to map file");
close(fd);
return nullptr;
}
std::shared_ptr<char> textbuf(new char[filesize + 1], ArrayDeleter<char>());
if (textbuf)
{
memcpy(textbuf.get(), filebuf.get(), filesize);
textbuf.get()[filesize] = '\0';
}
close(fd);
return textbuf;
}
#endif