-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbenchmark.coffee
76 lines (58 loc) · 1.77 KB
/
benchmark.coffee
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
helpers = require './test/helpers'
{randomInt, randomStr, addHelpers} = helpers
Rope = addHelpers(require '.')
#helpers.addHelpers(Rope)
time = (fn, iterations) ->
start = Date.now()
fn() for [0...iterations]
Date.now() - start
timeprint = (fn, iterations, name) ->
console.log "Benchmarking #{iterations} iterations of #{name}..."
result = time fn, iterations
console.log "#{name} took #{result} ms. #{result / iterations} ms per iteration, #{(iterations/result)|0}k iterations per second"
iterations/result
permute = (r) ->
random = helpers.useRandomWithSeed 100
->
if random() < 0.95
# Insert.
text = randomStr(randomInt 2)
pos = randomInt(r.length + 1)
r.insert pos, text
else
# Delete
pos = randomInt(r.length)
length = Math.min(r.length - pos, randomInt(10))
r.del pos, length
testToString = ->
r = new Rope randomStr(20000)
console.log r.length
timeprint (-> r.toString()), 1000000, 'toString'
testSizes = ->
throw new Error "You need to uncomment the setSpliceSize line in Rope.coffee to use this test" unless Rope.setSpliceSize?
size = 4
while size < 20000
Rope.setSplitSize size
console.log "Split size #{size}"
r = new Rope()
timeprint permute(r), 100000, 'Rope'
r.stats()
size *= 2
testBias = ->
throw new Error "You need to uncomment the setBias line in Rope.coffee to use this test" unless Rope.setBias?
results = []
for bias in [0.1..0.99] by 0.02
Rope.setBias bias
r = new Rope()
results.push [bias, timeprint permute(r), 3000000, "Bias #{bias}"]
console.log ""
console.log results.map((v) -> v.join()).join('\n')
naiveTest = ->
r = new Rope()
iterations = 5000000
timeprint permute(r), iterations, 'Rope'
# timeprint permute(helpers.Str()), iterations, 'Str'
r.stats()
testBias()
# naiveTest()
# testToString()