Skip to content

Instantly share code, notes, and snippets.

@pragtobgists
Last active February 26, 2017 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pragtobgists/4e38f696912d01882f5b789e9d6c076b to your computer and use it in GitHub Desktop.
Save pragtobgists/4e38f696912d01882f5b789e9d6c076b to your computer and use it in GitHub Desktop.
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