Title / Description
Code require 'benchmark' def fib1(n) #return n<2 ? n : fib1(n-1)+fib1(n-2) if n<2 then return n else return fib1(n-1)+fib1(n-2) end end def fib2(n) i=1 j=0 n.times { j=i+j i=j-i } return j end def fib3(n) i=1 j=0 k=0 h=1 while n>0 if n%2!=0 t=j*h j=i*h+j*k+t i=i*k+t end t=h*h h=2*k*h+t k=k*k+t n/=2 end return j end Benchmark.bm (10){ |x| x.report("fib1time: ") { fib1(35)}} puts Benchmark.bm (10){ |x| x.report("fib1time: ") { fib1(35)}} puts Benchmark.bm (10){ |x| x.report("fib1time: ") { fib1(35)}} puts Benchmark.bm (10){ |y| y.report("fib2time: ") { fib2(208000)}} puts Benchmark.bm (10){ |z| z.report("fib3time: ") { fib3(4000000)}}
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code