-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibonacci.rb
58 lines (44 loc) · 1.15 KB
/
fibonacci.rb
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
require 'compsci/fibonacci'
require 'benchmark/ips'
include CompSci
# recursive benchmarks with low N
[10, 20, 30].each { |num|
Benchmark.ips do |b|
b.config time: 0.5, warmup: 0.05
b.report("Fibonacci.classic(#{num})") {
Fibonacci.classic(num)
}
b.report("Fibonacci.cache_recursive(#{num})") {
Fibonacci.cache_recursive(num)
}
b.report("Fibonacci.cache_iterative(#{num})") {
Fibonacci.cache_iterative(num)
}
b.report("Fibonacci.dynamic(#{num})") {
Fibonacci.dynamic(num)
}
b.report("Fibonacci.matrix(#{num})") {
Fibonacci.matrix(num)
}
b.compare!
end
}
# nonrecursive benchmarks with high N
[50, 100, 150, 200, 500, 1000, 2000, 5000].each { |num|
Benchmark.ips do |b|
b.config time: 0.5, warmup: 0.05
b.report("Fibonacci.cache_recursive(#{num})") {
Fibonacci.cache_recursive(num)
}
b.report("Fibonacci.cache_iterative(#{num})") {
Fibonacci.cache_iterative(num)
}
b.report("Fibonacci.dynamic(#{num})") {
Fibonacci.dynamic(num)
}
b.report("Fibonacci.matrix(#{num})") {
Fibonacci.matrix(num)
}
b.compare!
end
}