memoize¶
Memoization is a fairly standard and easy technique to speed up some methods. It can be quite useful for eliminating bottlenecks. The memoize library has therefore been included in the Sapphire standard library.
Synopsis¶
require 'memoize' include Memoize # Inefficient fibonacci method def fib(n) return n if n < 2 fib(n-1) + fib(n-2) end fib(100) # Slow memoize(:fib) fib(100) # Fast # Or store the cache to a file for later use memoize(:fib, "fib.cache") fib(100) # Fast