-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiming.js
53 lines (45 loc) · 1.21 KB
/
timing.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
/**
* Returns how long it took to run func() the first time, the total time
* taken after N runs and the average of those N runs.
*
* @param func Function whose execution is to be timed.
* @param N Number of times it is to be run.
* @return first time execution time, total execution time over N runs
* and average of those times in an array.
*/
function meanTime(func, N) {
// Initialize globals
var sum = 0;
var diff;
var diffFirst;
// Repeat running timeEx for N times
for (let i = 0 ; i < N; i++) {
diff = timeEx(func);
if (i == 0) {
diffFirst = diff;
}
sum += diff;
}
// Determine avg
var avg = sum / N;
return [diffFirst, sum, avg];
}
/**
* Time the execution of the non-argument function specified.
*
* @param func A function that takes no arguments and whose execution is to
* be timed.
* @return Take taken to execute func.
*/
function timeEx(func) {
// Start time
var start = new Date();
start = start.getMilliseconds();
func();
// End time
var end = new Date();
end = end.getMilliseconds();
// Difference
var diff = end-start;
return diff;
}