From: Joey Date: 2006-07-13T18:36:06+09:00 Subject: Re: Recursion and Ruby ------=_Part_9920_22215770.1152783363784 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Try CachedProc class CachedProc def initialize(&block) @proc = block @cache = Hash.new{|cache, args| cache[args] = @proc.call(*args) } end def call(*args) @cache[args] end alias_method :[], :call def method_missing(name, *args, &block) @proc.send(name, *args, &block) end end def cached_proc(&block) CachedProc.new(&block) end Daniel Schierbeck wrote that. j`ey http://www.eachmapinject.com On 7/13/06, Erik Veenstra wrote: > > Only the lambda version (versions 5) is much slower. Why is the > lambda version so much slower? I like the lambda version! > > gegroet, > Erik V. - http://www.erikveen.dds.nl/ > > ---------------------------------------------------------------- > > Version 5 195,6% > > ---------------------------------------------------------------- > > # VERSION 5 > > fib = lambda{|n| n<=1 ? 1 : fib[n-1] + fib[n-2]} > > 0.upto(30){|i| puts "The #{i} Fibonacci number is #{fib[i]}"} > > ---------------------------------------------------------------- > > > ------=_Part_9920_22215770.1152783363784--