-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoubling-ratio.experiment.js
61 lines (55 loc) · 1.43 KB
/
doubling-ratio.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
58
59
60
61
const { StdOut, StdRandom } = require('../../libs')
const { pad } = require('../../utils')
const { StopWatch } = require('../../adts')
const { ThreeSum } = require('../../algorithms')
/**
* DoublingRatio
* @classdesc Runs experiments counting time for ThreeSum algorithm.
* @see p. 177, 192
*/
class DoublingRatio {
/**
* 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()
}
/**
* Calculates the ratio of each running time
* with the previous when executing the ThreeSum
* algorithm, doubling the input size every time.
* @example
* ```sh
* $ node doubling-ratio.experiment.js
* 250 0.0 2.8
* 500 0.0 3.5
* 1000 0.3 6.5
* 2000 1.8 6.9
* 4000 14.5 8.2
* 8000 96.4 6.7
* 16000 768.1 8.0
* ...
* ```
*/
static main () {
let prev = this.timeTrial(125)
for (let n = 250; true; n *= 2) {
const time = this.timeTrial(n)
StdOut.println(`${pad(n, 7)} ${pad(time.toFixed(1), 7)} ${pad((time / prev).toFixed(1), 7)}`)
prev = time
}
}
}
// Execution
// ==============================
DoublingRatio.main()