Skip to content

Instantly share code, notes, and snippets.

@pragtobgists

pragtobgists/number.ex

Last active Feb 26, 2017
Embed
What would you like to do?
very dumb is even function benchmarked tco vs. no tco
defmodule Number do
def is_even?(0), do: true
def is_even?(n) do
!is_even?(n - 1)
end
def is_even_tco?(n, acc \\ true)
def is_even_tco?(0, acc), do: acc
def is_even_tco?(n, acc) do
is_even_tco?(n - 1, !acc)
end
end
number = 10_000_000
Benchee.run %{
"is_even?" => fn -> Number.is_even?(number) end,
"is_even_tco?" => fn -> Number.is_even_tco?(number) end,
}
tobi@happy ~/github/elixir_playground $ mix run bench/is_even.exs
Benchmarking is_even?...
Benchmarking is_even_tco?...
Name ips average deviation median
is_even? 10.26 97449.21μs (±0.50%) 97263.00μs
is_even_tco? 9.39 106484.48μs (±0.09%) 106459.50μs
Comparison:
is_even? 10.26
is_even_tco? 9.39 - 1.09x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.