Created
January 17, 2010 22:48
-
-
Save headius/279641 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'inline' | |
require 'duby_inline' | |
require 'benchmark' | |
class FastMath | |
def factorial_ruby(n) | |
f = 1 | |
n.downto(2) { |x| f *= x } | |
return f | |
end | |
def fib_ruby(n) | |
if n < 2 | |
n | |
else | |
fib_ruby(n - 2) + fib_ruby(n - 1) | |
end | |
end | |
inline :Duby do |builder| | |
builder.duby " | |
def factorial_duby(max:int) | |
i = max | |
result = 1 | |
while i >= 2; result *= i-=1; end | |
result | |
end | |
" | |
builder.duby " | |
def fib_duby(n:int) | |
if n < 2 | |
n | |
else | |
fib_duby(n - 2) + fib_duby(n - 1) | |
end | |
end | |
" | |
end | |
end | |
math = FastMath.new | |
Benchmark.bmbm(30) {|bm| | |
5.times { bm.report("factorial_ruby") { 30000.times { math.factorial_ruby(30) } } } | |
5.times { bm.report("factorial_duby") { 30000.times { math.factorial_duby(30) } } } | |
5.times { bm.report("fib_ruby(35)") { math.fib_ruby(35) } } | |
5.times { bm.report("fib_duby(35)") { math.fib_duby(35) } } | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
New JDK: 1.6.0 | |
Rehearsal ----------------------------------------------------------------- | |
factorial_ruby 0.535000 0.000000 0.535000 ( 0.535000) | |
factorial_ruby 0.179000 0.000000 0.179000 ( 0.178000) | |
factorial_ruby 0.176000 0.000000 0.176000 ( 0.176000) | |
factorial_ruby 0.174000 0.000000 0.174000 ( 0.174000) | |
factorial_ruby 0.170000 0.000000 0.170000 ( 0.170000) | |
factorial_duby 0.229000 0.000000 0.229000 ( 0.229000) | |
factorial_duby 0.016000 0.000000 0.016000 ( 0.017000) | |
factorial_duby 0.019000 0.000000 0.019000 ( 0.019000) | |
factorial_duby 0.016000 0.000000 0.016000 ( 0.016000) | |
factorial_duby 0.018000 0.000000 0.018000 ( 0.018000) | |
fib_ruby(35) 3.192000 0.000000 3.192000 ( 3.192000) | |
fib_ruby(35) 2.992000 0.000000 2.992000 ( 2.992000) | |
fib_ruby(35) 3.009000 0.000000 3.009000 ( 3.009000) | |
fib_ruby(35) 3.001000 0.000000 3.001000 ( 3.001000) | |
fib_ruby(35) 2.988000 0.000000 2.988000 ( 2.988000) | |
fib_duby(35) 0.082000 0.000000 0.082000 ( 0.081000) | |
fib_duby(35) 0.077000 0.000000 0.077000 ( 0.077000) | |
fib_duby(35) 0.078000 0.000000 0.078000 ( 0.078000) | |
fib_duby(35) 0.078000 0.000000 0.078000 ( 0.078000) | |
fib_duby(35) 0.078000 0.000000 0.078000 ( 0.078000) | |
------------------------------------------------------- total: 17.107000sec | |
user system total real | |
factorial_ruby 0.179000 0.000000 0.179000 ( 0.179000) | |
factorial_ruby 0.176000 0.000000 0.176000 ( 0.176000) | |
factorial_ruby 0.174000 0.000000 0.174000 ( 0.174000) | |
factorial_ruby 0.169000 0.000000 0.169000 ( 0.168000) | |
factorial_ruby 0.170000 0.000000 0.170000 ( 0.170000) | |
factorial_duby 0.027000 0.000000 0.027000 ( 0.027000) | |
factorial_duby 0.023000 0.000000 0.023000 ( 0.023000) | |
factorial_duby 0.017000 0.000000 0.017000 ( 0.017000) | |
factorial_duby 0.022000 0.000000 0.022000 ( 0.022000) | |
factorial_duby 0.032000 0.000000 0.032000 ( 0.032000) | |
fib_ruby(35) 3.034000 0.000000 3.034000 ( 3.034000) | |
fib_ruby(35) 3.070000 0.000000 3.070000 ( 3.069000) | |
fib_ruby(35) 3.113000 0.000000 3.113000 ( 3.113000) | |
fib_ruby(35) 3.142000 0.000000 3.142000 ( 3.143000) | |
fib_ruby(35) 3.062000 0.000000 3.062000 ( 3.062000) | |
fib_duby(35) 0.080000 0.000000 0.080000 ( 0.080000) | |
fib_duby(35) 0.080000 0.000000 0.080000 ( 0.080000) | |
fib_duby(35) 0.079000 0.000000 0.079000 ( 0.079000) | |
fib_duby(35) 0.079000 0.000000 0.079000 ( 0.080000) | |
fib_duby(35) 0.080000 0.000000 0.080000 ( 0.080000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment