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
|
/* benchmark.vala
*
* Copyright (C) 2008 Jürg Billeter
* Copyright (C) 2009 Didier Villevalois
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library 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
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Didier 'Ptitjes Villevalois <ptitjes@free.fr>
*/
using Gee;
namespace Gee.Benchmark {
OptionEntry run_benchmark_option(string long_name, char short_name, string description, ref bool do_run) {
return OptionEntry() {
long_name = long_name,
short_name = short_name,
flags = 0,
arg = OptionArg.NONE,
arg_data = &do_run,
description = description,
arg_description = null
};
}
int main (string[] args) {
bool run_sort = false;
OptionEntry[] entries = {
run_benchmark_option("run-sort", 's', "Run sorting benchmark", ref run_sort)
};
var context = new OptionContext ("Run various benchmarks");
context.add_main_entries (entries, "gee-benchmark");
try {
context.parse (ref args);
} catch (OptionError e) {
stdout.printf ("option parsing failed: %s\n", e.message);
return 2;
}
if(run_sort) {
benchmark_sorts ();
}
return 0;
}
public interface Factory<G> : Object {
public abstract Collection<G> create ();
public abstract Collection<G> copy (Collection<G> collection);
}
public interface Generator<G> : Object {
public abstract string name { get; }
public abstract void generate_collection (int size, Collection<G> collection);
}
public interface Algorithm<G> : Object {
public abstract string name { get; }
public abstract void process_collection (Collection<G> collection);
}
public class RandomInt32 : Object, Generator<int32> {
public string name { get { return "FullRandom"; } }
public void generate_collection (int size, Collection<int32> collection) {
for (int i = 0; i < size; i++) {
collection.add (GLib.Random.int_range (0, size - 1));
}
}
}
public class FixedVarianceInt32 : Object, Generator<int32> {
public string name { get { return "FixedVariance"; } }
public void generate_collection (int size, Collection<int32> collection) {
int variance = (int) Math.sqrt (size);
for (int i = 0; i < size; i++) {
collection.add (i + GLib.Random.int_range (0, variance) - variance / 2);
}
}
}
public class MountsInt32 : Object, Generator<int32> {
public string name { get { return "Mounts"; } }
public void generate_collection (int size, Collection<int32> collection) {
int index = 0;
int last = 0;
int variance = (int) Math.sqrt (size);
while (index < size) {
int width = GLib.Random.int_range (0, variance);
int height = GLib.Random.int_range (- variance / 2, variance / 2);
for (int i = 0; i < width; i++) {
collection.add (last + height / width);
}
index += width;
last += height;
}
}
}
public class ReverseSortedInt32 : Object, Generator<int32> {
public string name { get { return "ReverseSorted"; } }
public void generate_collection (int size, Collection<int32> collection) {
for (int i = 0; i < size; i++) {
collection.add (size - i - 1);
}
}
}
public class SortedInt32 : Object, Generator<int32> {
public string name { get { return "Sorted"; } }
public void generate_collection (int size, Collection<int32> collection) {
for (int i = 0; i < size; i++) {
collection.add (i);
}
}
}
public class ArrayListFactory<G> : Object, Factory<G> {
public Collection<G> create () {
return new ArrayList<G> ();
}
public Collection<G> copy (Collection<G> collection) {
ArrayList<G> copy = new ArrayList<G> ();
foreach (G item in collection) {
copy.add (item);
}
return copy;
}
}
public class Benchmark<G> : Object {
public Benchmark (Factory<G> factory,
Gee.List<Algorithm<G>> algorithms,
Gee.List<Generator<G>> generators,
int[] sizes,
int iteration_count) {
this.factory = factory;
this.algorithms = algorithms;
this.sizes = sizes;
this.generators = generators;
this.iteration_count = iteration_count;
}
private Factory<G> factory;
private int[] sizes;
private Gee.List<Generator<G>> generators;
private Gee.List<Algorithm<G>> algorithms;
private int iteration_count;
private double[,,] results_sum;
private double[,,] results_squared_sum;
public void run () {
results_sum = new double[sizes.length,
generators.size,
algorithms.size];
results_squared_sum = new double[sizes.length,
generators.size,
algorithms.size];
for (int i = 0; i < sizes.length; i++) {
for (int j = 0; j < generators.size; j++) {
for (int k = 0; k < algorithms.size; k++) {
results_sum[i,j,k] = 0;
results_squared_sum[i,j,k] = 0;
}
}
}
Timer timer = new Timer ();
for (int iteration = 1; iteration <= iteration_count; iteration++) {
for (int i = 0; i < sizes.length; i++) {
int size = sizes[i];
for (int j = 0; j < generators.size; j++) {
Collection<G> collection = factory.create ();
generators[j].generate_collection (size, collection);
for (int k = 0; k < algorithms.size; k++) {
Collection<G> copy = factory.copy (collection);
timer.reset ();
timer.start ();
algorithms[k].process_collection (copy);
timer.stop ();
double elapsed = timer.elapsed ();
results_sum[i,j,k] += elapsed;
results_squared_sum[i,j,k] += Math.pow (elapsed, 2);
}
}
}
if (iteration % 10 == 0) {
stdout.printf ("|");
} else {
stdout.printf ("*");
}
stdout.flush ();
if (iteration % 100 == 0) {
stdout.printf ("\n\n");
display_results (iteration);
}
}
}
public void display_results (int iteration) {
stdout.printf ("After %d iterations: (average [sample standard deviation] in seconds)\n\n", iteration);
for (int i = 0; i < sizes.length; i++) {
stdout.printf ("%d elements:\n", sizes[i]);
stdout.printf ("%20s\t", "");
for (int k = 0; k < algorithms.size; k++) {
stdout.printf ("%-20s\t", algorithms[k].name);
}
stdout.printf ("\n");
for (int j = 0; j < generators.size; j++) {
stdout.printf ("%20s\t", generators[j].name);
for (int k = 0; k < algorithms.size; k++) {
double average = results_sum[i,j,k] / iteration;
double squared_deviation =
(results_squared_sum[i,j,k]
- ((double) iteration) * Math.pow (average, 2))
/ (iteration - 1);
double deviation = Math.sqrt (squared_deviation);
stdout.printf ("%8f [%8f] \t", average, deviation);
}
stdout.printf ("\n");
}
stdout.printf ("\n");
}
stdout.printf ("\n\n");
}
}
}
|