Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added benchmark and fixed recur spec

  • Loading branch information...
commit abdb4e0177723027845605c8116deb59bf3db5c5 1 parent 33caf67
@hosiawak authored
View
35 benchmark/bench_recur.rb
@@ -0,0 +1,35 @@
+require 'benchmark'
+require 'benchmark/ips'
+
+class RecurBench
+ def recur_fib(n)
+ if n == 0
+ 0
+ else
+ recur(n - 1)
+ end
+ end
+
+ def normal_fib(n)
+ if n == 0
+ 0
+ else
+ normal_fib(n - 1)
+ end
+ end
+end
+#
+@b = Recurbench.new
+
+Benchmark.ips do |x|
+ x.compare!
+
+ x.report "normal_fib" do
+ @b.normal_fib(9000)
+ end
+
+ x.report "recur_fib" do
+ @b.recur_fib(9000)
+ end
+
+end
View
18 benchmark/bench_symbol_to_proc.rb
@@ -0,0 +1,18 @@
+require 'benchmark'
+require 'benchmark/ips'
+
+Benchmark.ips do |x|
+ x.compare!
+
+ @s = :to_s
+ x.report "normal symbol to proc" do
+ [1].map(&@s)
+ end
+
+ x.report "rewritten symbol to proc" do
+ [1].map(&:to_s)
+ end
+
+
+
+end
View
16 lib/rubinius_macros.rb
@@ -1,10 +1,12 @@
-require 'lib/gensym'
-require 'lib/swap'
-require 'lib/symbol_to_proc'
-require 'lib/string_to_proc'
-require 'lib/andand'
-require 'lib/goto'
-require 'lib/recur'
+base = File.dirname(__FILE__)
+
+require base + '/gensym'
+require base + '/swap'
+require base + '/symbol_to_proc'
+require base + '/string_to_proc'
+require base + '/andand'
+require base + '/goto'
+require base + '/recur'
module Macros
View
18 spec/recur_spec.rb
@@ -1,7 +1,7 @@
describe "Recur macro" do
class RecurTest
- def factorial(n, product)
+ def factorial(n, product=1)
if n == 0
product
else
@@ -9,7 +9,7 @@ def factorial(n, product)
end
end
- def fib(n, x, y)
+ def fib(n, x=0, y=1)
if n == 0
x
else
@@ -17,6 +17,15 @@ def fib(n, x, y)
end
end
+ def regular_fib(n, x=0, y=1)
+ if n == 0
+ x
+ else
+ regular_fib(n - 1, y, x + y)
+ end
+ end
+
+
def default_arg_simple(a = 1)
a == 10 ? :ok : recur(a + 1)
end
@@ -55,11 +64,12 @@ def required_and_splat_factorial(n, *ary)
end
it "rebinds variables to passed args and goes to the start of the method" do
- @t.factorial(4,1).should == 24
+ @t.factorial(4).should == 24
end
it "doesn't blow the stack with too many call frames" do
- @t.fib(10_000, 0, 1).to_s[0...10].should == "3364476487"
+ lambda { @t.regular_fib(20_000) }.should raise_error(StackError)
+ lambda { @t.fib(20_000) }.should_not raise_error(StackError)
end
context "optional args" do

0 comments on commit abdb4e0

Please sign in to comment.
Something went wrong with that request. Please try again.