DefMemo

A memoization macro (defmemo) for Elixir.

Build Status

Adapted from : (Gustavo Brunoro) https://gist.github.com/brunoro/6159378

I found Gustavo's Gist when looking at memoization and elixir and fixed it to work with version 1.0.x. Since then I've fixed a few of the problems with the original implementation:

Usage

Add defmemo to your mix.exs file:

{:defmemo, "~> 0.1.0"}

And run:

mix deps.get

Before using a defmemo'd function start_link must be called. e.g.

DefMemo.start_link

or you can add :defmemo into the applications section of your mix.exs:

[applications: [:logger, :defmemo]]

Example

defmodule FibMemo do
import DefMemo
defmemo fibs(0), do: 0
defmemo fibs(1), do: 1
defmemo fibs(n), do: fibs(n - 1) + fibs(n - 2)
def fib_10 do
fibs(10)
end
end

Performance

More or less what you would expect:

UNMEMOIZED VS MEMOIZED
***********************
fib (unmemoized)
function -> {result, running time(μs)}
==================================
fibs(30) -> {832040, 31364}
fibs(30) -> {832040, 31281}
FibMemo (memoized)
==================================
fibs(30) -> {832040, 975}
fibs(30) -> {832040, 8}
fibs(50) -> {12586269025, 176}
fibs(50) -> {12586269025, 7}

TODO