From: Erik Veenstra Date: 2006-07-13T22:18:39+09:00 Subject: Re: Recursion and Ruby > Try CachedProc I could easily implement a cache myself, even a cleaner one (see below), but that's not my point. My point is: Why is a lambda call much slower then a method call? gegroet, Erik V. - http://www.erikveen.dds.nl/ ---------------------------------------------------------------- # Without cache. fib = lambda{|n| n<=1 ? 1 : fib[n-1] + fib[n-2]} 0.upto(30){|i| puts "The #{i} Fibonacci number is #{fib[i]}"} ---------------------------------------------------------------- # With cache. fib = lambda{|n| n<=1 ? 1 : fib[n-1] + fib[n-2]} cache = lambda{|f| h={} ; lambda{|*args| h[*args] ||= f[*args]}} fib = cache[fib] 0.upto(30){|i| puts "The #{i} Fibonacci number is #{fib[i]}"} ----------------------------------------------------------------