-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathmap_put_vs_put_in.exs
More file actions
59 lines (52 loc) · 1.19 KB
/
map_put_vs_put_in.exs
File metadata and controls
59 lines (52 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
defmodule MapPut.Fast do
def map_put(enumerator, map) do
enumerator
|> Enum.reduce(map, fn value, acc ->
Map.put(acc, value, value)
end)
end
end
defmodule MapPut.Slower do
def map_put(enumerator, map) do
enumerator
|> Enum.reduce(map, fn value, acc ->
put_in(acc[value], value)
end)
end
end
defmodule MapPut.Slowest do
def map_put(enumerator, map) do
enumerator
|> Enum.reduce(map, fn value, acc ->
put_in(acc, [value], value)
end)
end
end
defmodule MapPut.Benchmark do
@inputs %{
"Large (30,000 items)" => 1..10_000,
"Medium (3,000 items)" => 1..1_000,
"Small (30 items)" => 1..10
}
def benchmark do
Benchee.run(
%{
"Map.put/3" => fn enumerator -> bench_func(enumerator, MapPut.Fast) end,
"put_in/2" => fn enumerator -> bench_func(enumerator, MapPut.Slower) end,
"put_in/3" => fn enumerator -> bench_func(enumerator, MapPut.Slowest) end
},
time: 10,
inputs: @inputs,
print: [fast_warning: false]
)
end
@map %{
a: 1,
b: 2,
c: 3
}
def bench_func(enumerator, module) do
module.map_put(enumerator, @map)
end
end
MapPut.Benchmark.benchmark()