-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.js
229 lines (197 loc) · 4.55 KB
/
benchmark.js
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
// Benchmark of Pararr.js
// Run with:
// >node benchmark.js
var p = require('./lib/pararr'),
arr = [],
i,
// The factorial function
// For large numbers this is quite time consuming and hence a good candidate
// for parallelization
fac = function(a) {
return (function facI(a) {
if (a <= 1) {
return a;
}
return facI(a - 1);
})(a);
},
// Simple square function
x2 = function(a) {
return a * a;
},
// Simple funciton that checks if a number is even
isEven = function(a) {
return a % 2 === 0;
},
// Naive implementation of a primality check. Also a good candidate for
// Parallelization
isPrime = function(a) {
var i;
if (a <= 2) {
return true;
}
for (i = Math.ceil(Math.sqrt(a)) ; i > 1; i--) {
if (a % i === 0) {
return false;
}
}
return true;
},
// Simple timer
timerPrototype = {
start : function() {
this.begin = new Date();
},
stop : function() {
this.end = new Date();
return this.end - this.begin;
},
time : function() {
return this.end - this.begin;
}
};
// Factory function to create a timer object
function createTimer() {
var timer = Object.create(timerPrototype);
return timer;
}
// Check for array equality
function arrays_equal(a,b) {
return !!a && !!b && !(a<b || b<a);
}
// Benchmark paralell and normal array operations
function benchmarkArrFunc(op, arrLen, iter, iterName, cb) {
var t1, t2, //Timers
r1, r2, //Results
i;
console.log('Iterator: "' + iterName + '"');
console.log('# Elements: "' + arrLen + '"');
//Initialize pararr
p.init();
// Generate test data
for (i = 0; i < arrLen; i++) {
arr.push(Math.floor(Math.random() * 15000));
}
// Benchmark built in map function
t1 = createTimer();
t1.start();
r1 = arr[op](iter);
console.log(' Native ' + op + ': ' + t1.stop() + 'ms');
// Benchmark pararr
t2 = createTimer();
t2.start();
p[op](arr, iter, function(err, act) {
console.log(' Pararr ' + op + ': ' + t2.stop() + 'ms' +
(t2.time() < t1.time() ? ' - faster' : ' - slower'));
r2 = act;
if (!arrays_equal(r1,r2)) {
console.log(' ERROR: Arrays are not equal');
}
console.log(); //Empty line
p.destroy();
cb();
});
}
// Benchmark the parallel execution using pararr in comparision with sequential
function benchmarkParallel(cb) {
// Benchmark parallel
var t1 = createTimer(),
t2 = createTimer(), // Timers
x = 12345678912345678; // A number big enough to make a difference
p.init();
console.log('\nBenchmarking parallel (this may take some time)');
// Sequential execution of 4 isPrime
t1.start();
isPrime(x);
isPrime(x);
isPrime(x);
isPrime(x);
t1.stop();
console.log('Sequential execution: ' + t1.time() + 'ms');
// Parallel execution of 4 isPrime
t2.start();
p.parallel(
[
{
func: isPrime,
par: x
},
{
func: isPrime,
par: x
},
{
func: isPrime,
par: x
},
{
func: isPrime,
par: x
}
],
// Callback
function(err, result) {
t2.stop();
console.log('Parallel execution: ' + t2.time() + 'ms' +
(t2.time() < t1.time() ? ' - faster' : ' - slower'));
cb();
}
);
}
// Benchmark thes parallel sort in comparision to the native sort
function benchmarkSort(cb) {
var arr = [],
exp,
i, j,
N = 10000,
alphabet = 'abcdefghijklmnopqrstuvwxyz',
t1 = createTimer(),
t2 = createTimer(),
// The comparator that we will use
comp = function(a,b) {
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
};
console.log('\nBenchmarking sort()');
// Create demo data
for(i=0; i<N; i++) {
arr[i] = ''
for(j=0; j<300; j++) { // Very long words = Time consuming comparition = good use case
arr[i] += alphabet[Math.floor(Math.random() * alphabet.length)];
}
}
// Native sort
t1.start();
exp = arr.slice().sort();
t1.stop();
console.log('Native sort: ' + t1.time() + 'ms');
// Parallel sort
t2.start();
p.sort(arr, function(err, act) {
t2.stop();
console.log('Pararr sort: ' + t2.time() + 'ms' + (t2.time() < t1.time() ? ' - faster' : ' - slower'));
if (!arrays_equal(exp,act)) {
console.log('ERROR: Arrays are not equal');
}
cb();
});
}
// Run the benchmarks one after another
benchmarkArrFunc('map', 10000, fac, 'factorial', function() {
benchmarkArrFunc('map', 10000, x2, 'x^2', function() {
benchmarkArrFunc('filter', 100000, isPrime, 'isPrime', function() {
benchmarkArrFunc('filter', 100000, isEven, 'isEven', function() {
benchmarkParallel(function() {
benchmarkSort(function() {
p.destroy(); // Cleanup
});
});
});
});
});
});