-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathconcat_vs_cons.exs
More file actions
62 lines (54 loc) · 1.48 KB
/
concat_vs_cons.exs
File metadata and controls
62 lines (54 loc) · 1.48 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
60
61
62
defmodule ListAdd.Fast do
def add_lists(enumerator, list) do
enumerator
|> Enum.reduce([0], fn _, acc ->
[acc | list]
end)
|> List.flatten()
end
end
defmodule ListAdd.Medium do
def add_lists(enumerator, list) do
enumerator
|> Enum.reduce([0], fn _, acc ->
[list | acc]
end)
|> Enum.reverse()
|> List.flatten()
end
end
defmodule ListAdd.Slow do
def add_lists(enumerator, list) do
Enum.reduce(enumerator, [0], fn _, acc ->
acc ++ list
end)
end
end
defmodule ListAdd.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(
%{
"Cons + Flatten" => fn enumerator -> bench_func(enumerator, ListAdd.Fast) end,
"Cons + Reverse + Flatten" => fn enumerator -> bench_func(enumerator, ListAdd.Medium) end,
"Concatenation" => fn enumerator -> bench_func(enumerator, ListAdd.Slow) end
},
time: 10,
inputs: @inputs,
print: [fast_warning: false]
)
end
@list [1, 2, 3]
def bench_func(enumerator, module) do
module.add_lists(enumerator, @list)
end
end
#expected = [0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
#IO.inspect(ListAdd.Fast.add_lists(0..4, [1, 2, 3]) == expected)
#IO.inspect(ListAdd.Slow.add_lists(0..4, [1, 2, 3]) == expected)
#IO.inspect(ListAdd.Medium.add_lists(0..4, [1, 2, 3]) == expected)
ListAdd.Benchmark.benchmark()