-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoubling-test.experiment.js
57 lines (52 loc) · 1.21 KB
/
doubling-test.experiment.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
const { StdOut, StdRandom } = require('../../libs')
const { pad } = require('../../utils')
const { StopWatch } = require('../../adts')
const { ThreeSum } = require('../../algorithms')
/**
* DoublingTest
* @classdesc Runs experiments counting time for ThreeSum algorithm.
* @see p. 177
*/
class DoublingTest {
/**
* Returns the time elapsed
* counting the triplets in `n`
* random ints.
* @param {number} n Array size.
*/
static timeTrial (n) {
const MAX = 1000000
const a = []
for (let i = 0; i < n; i++) {
a[i] = StdRandom.uniform(-MAX, MAX)
}
const timer = new StopWatch()
ThreeSum.count(a)
return timer.elapsedTime()
}
/**
* Tracks the time of ThreeSum algorithm
* doubling the size of array a every time.
* @example
* ```sh
* $ node doubling-test.experiment.js
* 250 0.013
* 500 0.043
* 1000 0.298
* 2000 2.418
* 4000 19.371
* 8000 96.712
* 16000 722.276
* ...
* ```
*/
static main () {
for (let i = 250; ; i *= 2) {
const time = this.timeTrial(i)
StdOut.println(`${pad(i, 7)} ${pad(time, 7)}`)
}
}
}
// Execution
// ==============================
DoublingTest.main()