-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathconcat_vs_cons.exs
67 lines (59 loc) · 1.62 KB
/
concat_vs_cons.exs
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
63
64
65
66
67
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
@small_list Enum.to_list(1..10)
@large_list Enum.to_list(1..1_000)
@inputs %{
"1,000 small items" => {1..1_000, @small_list},
"100 small items" => {1..100, @small_list},
"10 small items" => {1..10, @small_list},
"1,000 large items" => {1..1_000, @large_list},
"100 large items" => {1..1000, @large_list},
"10 large items" => {1..10, @large_list},
}
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
def bench_func({enumerator, list}, module) do
module.add_lists(enumerator, list)
end
end
# Enum.each([ListAdd.Slow, ListAdd.Medium, ListAdd.Fast], fn module ->
# IO.inspect(
# [0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3] == module.add_lists(0..4, [1, 2, 3])
# )
# end)
ListAdd.Benchmark.benchmark()