-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.rb
61 lines (47 loc) · 1.09 KB
/
bench.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
59
60
61
#RubyVM::InstructionSequence.compile_option = { tailcall_optimization: true }
# frozen_string_literal: true
require 'json'
require_relative 'lib/lexer'
require_relative 'lib/parser'
require_relative 'lib/interpreter'
def patropi(program)
lexer = Lexer.new(program)
parser = Parser.new(lexer)
parser.parse!
Interpreter.run({ expression: parser.ast }.to_json)
end
rinha = -> (number) {
<<~PROGRAM
let fib = fn (n, a, b) => {
if (n == 0) {
a
} else {
fib(n - 1, b, a + b)
}
};
let _ = fib(#{number}, 0, 1);
print("fib(#{number}) done.")
PROGRAM
}
def fib(n, a, b)
return a if n == 0
fib(n - 1, b, a + b)
end
def fib_lambda(n, a, b)
return a if n == 0
-> { fib_lambda(n - 1, b, a + b) }
end
def fib_trampoline(n)
result = fib_lambda(n, 0, 1)
while result.is_a?(Proc)
result = result.call
end
result
end
require 'benchmark'
number = 10_000
Benchmark.bm do |x|
x.report('patropi') { patropi(rinha.call(number)) }
#x.report('ruby_tco') { fib(number, 0, 1) }
x.report('ruby_trampoline') { fib_trampoline(number) }
end