Last active
February 26, 2017 10:33
-
-
Save pragtobgists/4e38f696912d01882f5b789e9d6c076b to your computer and use it in GitHub Desktop.
very dumb is even function benchmarked tco vs. no tco
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
number = 10_000_000 | |
Benchee.run %{ | |
"is_even?" => fn -> Number.is_even?(number) end, | |
"is_even_tco?" => fn -> Number.is_even_tco?(number) end, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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