Skip to content

Instantly share code, notes, and snippets.

@cr0t
Created July 20, 2024 10:25
Show Gist options
  • Save cr0t/68172f6ebf4b44d3acb0a973ed633a7e to your computer and use it in GitHub Desktop.
Save cr0t/68172f6ebf4b44d3acb0a973ed633a7e to your computer and use it in GitHub Desktop.
Compare access times to the elements in a Tuple, a Map, or a List containers
Mix.install([:benchee])
# Compare access times to the elements in a Tuple, a Map, or a List containers.
#
# At first, we generate some input that is being used in the access_* functions:
# they go over each element and check if it is what we put there. One by one.
defmodule Accessor do
@elements_n 10_000
def generate_input() do
{a_list, a_map} = Enum.reduce(0..@elements_n, {[], %{}}, fn i, {a_list, a_map} ->
new_list = [i | a_list]
new_map = Map.put(a_map, i, i)
{new_list, new_map}
end)
a_list = Enum.reverse(a_list)
a_tuple = List.to_tuple(a_list)
{a_list, a_map, a_tuple}
end
def access_list(a_list) do
Enum.each(0..@elements_n, fn i ->
if Enum.at(a_list, i) != i, do: raise "wrong item in the list"
end)
end
def access_map(a_map) do
Enum.each(0..@elements_n, fn i ->
if a_map[i] != i, do: raise "wrong item in the map"
end)
end
def access_tuple(a_tuple) do
Enum.each(0..@elements_n, fn i ->
if elem(a_tuple, i) != i, do: raise "wrong item in the tuple"
end)
end
end
{a_list, a_map, a_tuple} = Accessor.generate_input()
Benchee.run(
%{
"list" => fn -> Accessor.access_list(a_list) end,
"map" => fn -> Accessor.access_map(a_map) end,
"tuple" => fn -> Accessor.access_tuple(a_tuple) end
}
)

Elements in the input: 10

Name            ips        average  deviation         median         99th %
tuple        6.47 M      154.46 ns ±64825.56%          83 ns         167 ns
map          4.45 M      224.55 ns ±29494.61%         167 ns         292 ns
list         1.74 M      575.44 ns ±13139.21%         209 ns       12125 ns

Comparison:
tuple        6.47 M
map          4.45 M - 1.45x slower +70.09 ns
list         1.74 M - 3.73x slower +420.98 ns

Elements in the input: 1000

Name            ips        average  deviation         median         99th %
tuple      193.11 K        5.18 μs   ±199.94%        5.08 μs        5.63 μs
map         56.07 K       17.83 μs    ±32.52%       17.75 μs       18.79 μs
list         2.01 K      496.36 μs     ±7.03%      487.25 μs      628.72 μs

Comparison:
tuple      193.11 K
map         56.07 K - 3.44x slower +12.66 μs
list         2.01 K - 95.85x slower +491.18 μs

Elements in the input: 10_000

Name            ips        average  deviation         median         99th %
tuple       18.69 K       53.52 μs    ±13.96%       54.17 μs       65.33 μs
map          4.22 K      237.06 μs    ±11.99%      240.83 μs      269.78 μs
list       0.0211 K    47308.38 μs     ±0.89%    47316.63 μs    48630.34 μs

Comparison:
tuple       18.69 K
map          4.22 K - 4.43x slower +183.55 μs
list       0.0211 K - 884.00x slower +47254.86 μs

Elements in the input: 100_000

Name            ips        average  deviation         median         99th %
tuple       1924.47      0.00052 s     ±4.97%      0.00051 s      0.00057 s
map          337.55      0.00296 s     ±0.52%      0.00296 s      0.00301 s
list           0.23         4.28 s     ±1.69%         4.28 s         4.33 s

Comparison:
tuple       1924.47
map          337.55 - 5.70x slower +0.00244 s
list           0.23 - 8239.45x slower +4.28 s
@oezg
Copy link

oezg commented Jul 20, 2024

Thank you for this example.

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