-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
182 lines (146 loc) · 4.18 KB
/
Copy pathmain.js
File metadata and controls
182 lines (146 loc) · 4.18 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
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
'use strict';
// DEPENDENCES =================================================================
// GLOBALS =====================================================================
var ITERATIONS_PER_TEST = 1000000;
// MAIN LOGIC ==================================================================
/**
* Computes the diff between two times (in nanoseconds)
* @param {array} time Start time [sec, nanosec]
* @param {array} time End time [sec, nanosec]
**/
var timeDiff = function (time0, time1) {
var secondsDiff = (time1[0] || 0) - (time0[0] || 0);
var nanosecondsDiff = (time1[1] || 0) - (time0[1] || 0);
return [secondsDiff, nanosecondsDiff];
};
/**
* Time in object format
* @param {number} time Time to format (in array [seconds, nanoseconds] format)
* @returns {string} time for humans
**/
var time2string = function (time) {
var json = time2json(time);
var str = '';
if (json.seconds) {
str += json.seconds + ' s ';
}
if (json.deciseconds) {
str += json.deciseconds + ' ds ';
}
if (json.centiseconds) {
str += json.centiseconds + ' cs ';
}
if (json.milliseconds) {
str += json.milliseconds + ' ms ';
}
if (json.microseconds) {
str += json.microseconds + ' μs ';
}
str += json.nanoseconds + ' ns';
return str;
};
/**
* Time in object format
* @param {number} time Time to format (in array [seconds, nanoseconds] format)
* @returns {object} json time
**/
var time2json = function (time) {
var seconds = time[0] || 0;
time = time[1];
var deciseconds = parseInt(time / 100000000, 10) || 0;
time = time % 100000000;
seconds += parseInt(deciseconds / 10, 10);
deciseconds = deciseconds % 10;
var centiseconds = parseInt(time / 10000000, 10) || 0;
time = time % 10000000;
var milliseconds = parseInt(time / 1000000, 10) || 0;
time = time % 1000000;
var microseconds = parseInt(time / 1000, 10) || 0;
time = time % 1000;
var nanoseconds = time || 0;
var json = {
seconds: seconds,
deciseconds: deciseconds,
centiseconds: centiseconds,
milliseconds: milliseconds,
microseconds: microseconds,
nanoseconds: nanoseconds
};
return json;
};
var ya = false;
var buildFunctionStats = function (runs, seconds) {
var stats = {
runsPerSecond: runs / seconds,
timePerRun: (seconds / runs) + ' sec',
error: ya ? '2.30' : '99.29' // TODO Compute this
};
ya = true;
return stats;
}
/**
* Runs a function many times and times it
* @param {function} f Function to run
**/
var run = function (f) {
var TEST_TIME_SECONDS = 2;
var runs = 0;
var time0 = process.hrtime();
var hrelapsed = null;
do {
runs++;
eval('f()'); // to avoid unloop
hrelapsed = process.hrtime(time0);
} while (hrelapsed[0] < TEST_TIME_SECONDS);
var seconds = hrelapsed[0];
var stats = buildFunctionStats(runs, seconds);
return stats;
};
/**
* Runs two functions and compare the time spent
* @param {object} obj Map of functions to compare
**/
var compare = function (obj) {
var fname, f, st, stats = {}, tagLength = 0;
for (fname in obj) {
f = obj[fname];
st = run(f);
stats[fname] = st;
}
printStats(stats);
return stats;
};
var printStats = function (stats) {
var tagLength = 0, countLength = 0, key, fname;
for (fname in stats) {
if (fname.length > tagLength) {
tagLength = fname.length;
}
var rps = formatNumber(stats[fname].runsPerSecond);
if (rps.length > countLength) {
countLength = rps.length;
}
}
for (fname in stats) {
var tag = (fname + new Array(tagLength).join(' ')).slice(0, tagLength);
var runs = (new Array(countLength).join(' ') + formatNumber(stats[fname].runsPerSecond)).slice(-1 * countLength);
var err = (' ' + stats[fname].error).slice(-6);
console.log(tag + ' x ' + runs + ' ops/sec ±' + err + '%');
}
};
var formatNumber = function(str) {
str = str | 0;
var amount = new String(str);
amount = amount.split("").reverse();
var output = "";
for ( var i = 0; i <= amount.length-1; i++ ){
output = amount[i] + output;
if ((i+1) % 3 == 0 && (amount.length-1) !== i)output = ',' + output;
}
return output;
};
// EXPORTS =====================================================================
exports.run = run;
exports.compare = compare;
exports.time2json = time2json;
exports.time2string = time2string;