Last active
September 22, 2023 10:02
-
-
Save akoutmos/6e965558fa8bb5771e90b1961762a7d0 to your computer and use it in GitHub Desktop.
Elixir Benchmark of big versus small maps (inspired by https://twitter.com/akoutmos/status/1266034402422853633)
This file contains hidden or 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
# ---------------- Binary key ---------------- | |
bin_really_small_map = | |
1..8 | |
|> Enum.map(fn val -> {"key_#{val}", "#{val}"} end) | |
|> Map.new() | |
bin_small_map = | |
1..32 | |
|> Enum.map(fn val -> {"key_#{val}", "#{val}"} end) | |
|> Map.new() | |
bin_big_map = | |
1..33 | |
|> Enum.map(fn val -> {"key_#{val}", "#{val}"} end) | |
|> Map.new() | |
binary_tests = %{ | |
"Binary Really Small Map - first item" => fn -> Map.get(bin_really_small_map, "key_1") end, | |
"Binary Small Map - first item" => fn -> Map.get(bin_small_map, "key_1") end, | |
"Binary Large Map - first item" => fn -> Map.get(bin_big_map, "key_1") end, | |
"Binary Really Small Map - middle item" => fn -> Map.get(bin_really_small_map, "key_4") end, | |
"Binary Small Map - middle item" => fn -> Map.get(bin_small_map, "key_16") end, | |
"Binary Large Map - middle item" => fn -> Map.get(bin_big_map, "key_16") end, | |
"Binary Really Small Map - last item" => fn -> Map.get(bin_really_small_map, "key_8") end, | |
"Binary Small Map - last item" => fn -> Map.get(bin_small_map, "key_32") end, | |
"Binary Large Map - last item" => fn -> Map.get(bin_big_map, "key_32") end, | |
"Binary Really Small Map - invalid item" => fn -> Map.get(bin_really_small_map, "key_error") end, | |
"Binary Small Map - invalid item" => fn -> Map.get(bin_small_map, "key_error") end, | |
"Binary Large Map - invalid item" => fn -> Map.get(bin_big_map, "key_error") end | |
} | |
# ---------------- Integer key ---------------- | |
int_really_small_map = | |
1..8 | |
|> Enum.map(fn val -> {val, "#{val}"} end) | |
|> Map.new() | |
int_small_map = | |
1..32 | |
|> Enum.map(fn val -> {val, "#{val}"} end) | |
|> Map.new() | |
int_big_map = | |
1..33 | |
|> Enum.map(fn val -> {val, "#{val}"} end) | |
|> Map.new() | |
integer_tests = %{ | |
"Integer Really Small Map - first item" => fn -> Map.get(int_really_small_map, 1) end, | |
"Integer Small Map - first item" => fn -> Map.get(int_small_map, 1) end, | |
"Integer Large Map - first item" => fn -> Map.get(int_big_map, 1) end, | |
"Integer Really Small Map - middle item" => fn -> Map.get(int_really_small_map, 4) end, | |
"Integer Small Map - middle item" => fn -> Map.get(int_small_map, 16) end, | |
"Integer Large Map - middle item" => fn -> Map.get(int_big_map, 16) end, | |
"Integer Really Small Map - last item" => fn -> Map.get(int_really_small_map, 8) end, | |
"Integer Small Map - last item" => fn -> Map.get(int_small_map, 32) end, | |
"Integer Large Map - last item" => fn -> Map.get(int_big_map, 32) end, | |
"Integer Really Small Map - invalid item" => fn -> Map.get(int_really_small_map, 100) end, | |
"Integer Small Map - invalid item" => fn -> Map.get(int_small_map, 100) end, | |
"Integer Large Map - invalid item" => fn -> Map.get(int_big_map, 100) end | |
} | |
# ---------------- Atom key ---------------- | |
atom_really_small_map = | |
1..8 | |
|> Enum.map(fn val -> {String.to_atom("key_#{val}"), "#{val}"} end) | |
|> Map.new() | |
atom_small_map = | |
1..32 | |
|> Enum.map(fn val -> {String.to_atom("key_#{val}"), "#{val}"} end) | |
|> Map.new() | |
atom_big_map = | |
1..33 | |
|> Enum.map(fn val -> {String.to_atom("key_#{val}"), "#{val}"} end) | |
|> Map.new() | |
atom_tests = %{ | |
"Atom Really Small Map - first item" => fn -> Map.get(atom_really_small_map, :key_1) end, | |
"Atom Small Map - first item" => fn -> Map.get(atom_small_map, :key_1) end, | |
"Atom Large Map - first item" => fn -> Map.get(atom_big_map, :key_1) end, | |
"Atom Really Small Map - middle item" => fn -> Map.get(atom_really_small_map, :key_4) end, | |
"Atom Small Map - middle item" => fn -> Map.get(atom_small_map, :key_16) end, | |
"Atom Large Map - middle item" => fn -> Map.get(atom_big_map, :key_16) end, | |
"Atom Really Small Map - last item" => fn -> Map.get(atom_really_small_map, :key_8) end, | |
"Atom Small Map - last item" => fn -> Map.get(atom_small_map, :key_32) end, | |
"Atom Large Map - last item" => fn -> Map.get(atom_big_map, :key_32) end, | |
"Atom Really Small Map - invalid item" => fn -> Map.get(atom_really_small_map, :key_error) end, | |
"Atom Small Map - invalid item" => fn -> Map.get(atom_small_map, :key_error) end, | |
"Atom Large Map - invalid item" => fn -> Map.get(atom_big_map, :key_error) end | |
} | |
# ---------------- Benchee tests ---------------- | |
Benchee.run( | |
integer_tests, | |
warmup: 1, | |
time: 3 | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test results: