Created
July 2, 2019 17:44
-
-
Save cararemixed/8292f030e134ee7d985a08a7d87d8c66 to your computer and use it in GitHub Desktop.
Response to this post: http://stratus3d.com/blog/2019/06/29/performance-of-elixirs-access-behavior/
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
# Using the benchee library | |
keys = | |
1..12 | |
|> Enum.map(fn key -> | |
Integer.to_string(key) | |
end) | |
map = | |
keys | |
|> Enum.take_random(9) | |
|> Map.new(&{&1, true}) | |
times = 100_000 | |
# NOTE: The benchmark loops return :ok to explicitly | |
# avoid generating lists as garbage (the Erlang | |
# compiler will turn that into a loop and avoid | |
# generating a list entirely). | |
Benchee.run(%{ | |
# Enable this to compare the loop overhead itself | |
# "null" => fn -> | |
# for _key <- keys, _ <- 1..times, do: :ok | |
# :ok | |
# end, | |
"Map.get" => fn -> | |
for key <- keys, _ <- 1..times do | |
Map.get(map, key) | |
end | |
:ok | |
end, | |
"Access" => fn -> | |
for key <- keys, _ <- 1..times do | |
map[key] | |
end | |
:ok | |
end | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample results: