Skip to content

Instantly share code, notes, and snippets.

@J3RN
Created January 15, 2021 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save J3RN/51db9b64e51e83276a7bdb17bc720a92 to your computer and use it in GitHub Desktop.
Save J3RN/51db9b64e51e83276a7bdb17bc720a92 to your computer and use it in GitHub Desktop.
Benchmark of different methods of map transformation
sample = Enum.map(1..1000, &{&1, &1}) |> Map.new()
Benchee.run(%{
"maps:map" => fn -> :maps.map(fn _k, v -> v * 2 end, sample) end,
"Map.new" => fn -> Map.new(sample, fn {k, v} -> {k, v * 2} end) end,
"for" => fn -> for {k, v} <- sample, into: %{}, do: {k, v * 2} end
})
@J3RN
Copy link
Author

J3RN commented Jan 15, 2021

My results:

Operating System: Linux
CPU Information: AMD Ryzen 9 5950X 16-Core Processor
Number of Available Cores: 32
Available memory: 31.36 GB
Elixir 1.11.1
Erlang 23.1

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

Benchmarking Map.new...
Benchmarking for...
Benchmarking maps:map...

Name               ips        average  deviation         median         99th %
Map.new        15.45 K       64.74 μs    ±10.92%       65.19 μs       91.24 μs
maps:map       13.13 K       76.16 μs     ±5.93%       77.51 μs       89.57 μs
for            11.14 K       89.73 μs     ±9.05%       89.21 μs      112.04 μs

Comparison: 
Map.new        15.45 K
maps:map       13.13 K - 1.18x slower +11.42 μs
for            11.14 K - 1.39x slower +24.99 μs

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