-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm_timer.js
More file actions
68 lines (59 loc) · 1.83 KB
/
Copy pathalgorithm_timer.js
File metadata and controls
68 lines (59 loc) · 1.83 KB
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
"use strict";
const performance = require('perf_hooks').performance;
let custom = require('./custom_algorithms');
class AlgorithmTimer {
constructor() {
}
// returns array with time of execution for parsed function and the corresponding length of the array
recordTime(aFunction){
// array to apply our function under test
let array = [];
// array of the results of the test
let results = [];
// number of times the tests will be performed
const SAMPLES = 20;
// repeated call of the timer function
for(var i = 0; i < SAMPLES; i++){
timer(aFunction);
}
return calculateAverage(results);
// support routines to follow
function timer(aFunction){
if(results.length === SAMPLES - 1){
array = [];
}
for(var i = 0; i < SAMPLES; i++){
array = returnIncreasedArray(array);
var startTime = performance.now();
array[aFunction]();
var endTime = performance.now();
var item = [array.length, (endTime - startTime)]
if(results[i] === undefined){
results.push(item);
} else {
results[i][1] + item[1];
}
}
}
// increases the array size by 500
function returnIncreasedArray(array){
var low = array.length;
var high = low + 500;
for (var i = low; i < high; i++){
var newarray = [1,2,3,4,5,6,7,8,9,10];
var randomIndex = Math.floor(Math.random() * newarray.length);
array.push(newarray[randomIndex]);
}
array = custom.customShuffleLinear(array);
return array;
}
// returns the avaerage result across the samples runtime
function calculateAverage(results){
for(var i = 0; i < SAMPLES; i++){
results[i][1] = results[i][1] / SAMPLES;
}
return results;
}
}
}
module.exports = AlgorithmTimer