Skip to content

Instantly share code, notes, and snippets.

@rodrigues
Last active February 10, 2023 20:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodrigues/ccd8115c20944d0adb42a07aa017d972 to your computer and use it in GitHub Desktop.
Save rodrigues/ccd8115c20944d0adb42a07aa017d972 to your computer and use it in GitHub Desktop.
defmodule TTTMap do
def win?(%{s1: x, s2: x, s3: x}) when not is_nil(x), do: true
def win?(%{s4: x, s5: x, s6: x}) when not is_nil(x), do: true
def win?(%{s7: x, s8: x, s9: x}) when not is_nil(x), do: true
def win?(%{s1: x, s4: x, s7: x}) when not is_nil(x), do: true
def win?(%{s2: x, s5: x, s8: x}) when not is_nil(x), do: true
def win?(%{s3: x, s6: x, s9: x}) when not is_nil(x), do: true
def win?(%{s1: x, s5: x, s9: x}) when not is_nil(x), do: true
def win?(%{s3: x, s5: x, s7: x}) when not is_nil(x), do: true
def win?(_), do: false
end
defmodule TTTTuple do
def win?({x, x, x, _, _, _, _, _, _}) when not is_nil(x), do: true
def win?({_, _, _, x, x, x, _, _, _}) when not is_nil(x), do: true
def win?({_, _, _, _, _, _, x, x, x}) when not is_nil(x), do: true
def win?({x, _, _, x, _, _, x, _, _}) when not is_nil(x), do: true
def win?({_, x, _, _, x, _, _, x, _}) when not is_nil(x), do: true
def win?({_, _, x, _, _, x, _, _, x}) when not is_nil(x), do: true
def win?({x, _, _, _, x, _, _, _, x}) when not is_nil(x), do: true
def win?({_, _, x, _, x, _, x, _, _}) when not is_nil(x), do: true
def win?(_), do: false
end
defmodule TTTList do
def win?([x, x, x, _, _, _, _, _, _]) when not is_nil(x), do: true
def win?([_, _, _, x, x, x, _, _, _]) when not is_nil(x), do: true
def win?([_, _, _, _, _, _, x, x, x]) when not is_nil(x), do: true
def win?([x, _, _, x, _, _, x, _, _]) when not is_nil(x), do: true
def win?([_, x, _, _, x, _, _, x, _]) when not is_nil(x), do: true
def win?([_, _, x, _, _, x, _, _, x]) when not is_nil(x), do: true
def win?([x, _, _, _, x, _, _, _, x]) when not is_nil(x), do: true
def win?([_, _, x, _, x, _, x, _, _]) when not is_nil(x), do: true
def win?(_), do: false
end
opts = [nil, :o, :x]
tuple_items =
for s1 <- opts,
s2 <- opts,
s3 <- opts,
s4 <- opts,
s5 <- opts,
s6 <- opts,
s7 <- opts,
s8 <- opts,
s9 <- opts do
{s1, s2, s3, s4, s5, s6, s7, s8, s9}
end
list_items = Enum.map(tuple_items, &Tuple.to_list/1)
map_items =
for {s1, s2, s3, s4, s5, s6, s7, s8, s9} <- tuple_items do
%{s1: s1, s2: s2, s3: s3, s4: s4, s5: s5, s6: s6, s7: s7, s8: s8, s9: s9}
end
Benchee.run(
%{
"list" => fn -> Enum.each(list_items, &TTTList.win?/1) end,
"tuple" => fn -> Enum.each(tuple_items, &TTTTuple.win?/1) end,
"map" => fn -> Enum.each(map_items, &TTTMap.win?/1) end
},
warmup: 5,
time: 10
)
@rodrigues
Copy link
Author

➜ mix run bench/ttt.exs
Operating System: macOS
CPU Information: Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz
Number of Available Cores: 4
Available memory: 24 GB
Elixir 1.10.4
Erlang 22.3.4.3

Benchmark suite executing with the following configuration:
warmup: 5 s
time: 10 s
memory time: 0 ns
parallel: 1
inputs: none specified
Estimated total run time: 45 s

Benchmarking list...
Benchmarking map...
Benchmarking tuple...

Name            ips        average  deviation         median         99th %
tuple        680.41        1.47 ms    ±10.97%        1.47 ms        1.89 ms
list         537.31        1.86 ms     ±7.11%        1.85 ms        2.29 ms
map          352.46        2.84 ms     ±4.42%        2.81 ms        3.35 ms

Comparison:
tuple        680.41
list         537.31 - 1.27x slower +0.39 ms
map          352.46 - 1.93x slower +1.37 ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment