# [MacRuby-devel] Speed

Laurent Sansonetti lsansonetti at apple.com
Sun Nov 22 17:30:14 PST 2009

```Hi Uliano,

Thanks for trying MacRuby.

Vincent is right. Your second test is actually running too fast
(0.008167 seconds here) that most of the time is spent JIT compiling
the block. Try to loop 10 times around the test and you should see
that further calls are faster than Ruby 1.9.1. Or try to increase the
test value.

I don't believe this is unacceptable. JIT compilation is slow but here
0.008167 seconds are fast enough.

Laurent

On Nov 22, 2009, at 1:25 AM, Uliano Guerrini wrote:

> I'm new to Ruby BECAUSE of MacRuby
>
> While learning I run this simple exercise (the sum of even fibonacci
> less than 4 million) on both MacRuby 0.5.2 and ruby 1.9.1.
>
> the recursive version runs about 5 times faster on MacRuby and that
> was expected but the iterative runs about 1000!!! times slower, and
> that is *frankly* unacceptable.
>
> What's going on?
>
> uliano
>
> def fib_rec(n)
>  n < 2 ? n : fib_rec(n-1) + fib_rec(n-2)
> end
>
>
> def fib_iter(limit)
>  yield 0
>  yield 1
>  lastbut=0
>  last=1
>  while true
>    a=lastbut+last
>    if a<=limit
>      yield a
>    else
>      return
>    end
>    lastbut=last
>    last=a
>  end
> end
>
>
> sum=0
> i=0
> start=Time.now
> while (f=fib_rec(i))<=4_000_000
>  sum += f if f%2==0
>  i+=1
> end
> time=Time.now-start
>
> puts 'recursive'
> puts 'sum = ',sum
> puts 'time = ',time
>
> sum=0
> start=Time.now
> fib_iter(4_000_000) { |f| sum+=f if f%2==0}
> time=Time.now-start
>
> puts 'iterative'
> puts 'sum = ',sum
> puts 'time = ',time
>
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel

```