-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathcomparing_strings_vs_atoms.exs
68 lines (58 loc) · 1.58 KB
/
comparing_strings_vs_atoms.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
68
defmodule Compare.Fast do
def compare(first, second) do
first == second
end
end
defmodule Compare.Medium do
def compare(first, second) do
String.to_atom(first) == String.to_atom(second)
end
end
defmodule Compare.Slow do
def compare(first, second) do
first == second
end
end
defmodule Compare.Benchmark do
@inputs %{
"Large (1-100)" => :large,
"Medium (1-50)" => :medium,
"Small (1-5)" => :small
}
@strings_right %{
large: Enum.join(1..100),
medium: Enum.join(1..50),
small: Enum.join(1..5)
}
@strings_left %{
large: Enum.join(2..101),
medium: Enum.join(2..51),
small: Enum.join(2..6)
}
@atoms_right %{
large: 1..100 |> Enum.join |> String.to_atom,
medium: 1..50 |> Enum.join |> String.to_atom,
small: 1..5 |> Enum.join |> String.to_atom
}
@atoms_left %{
large: 2..101 |> Enum.join |> String.to_atom,
medium: 2..51 |> Enum.join |> String.to_atom,
small: 2..6 |> Enum.join |> String.to_atom
}
def benchmark do
Benchee.run(
%{
"Comparing atoms" => fn key -> bench_func(@atoms_left[key], @atoms_right[key], Compare.Fast) end,
"Converting to atoms and then comparing" => fn key -> bench_func(@strings_left[key], @strings_right[key], Compare.Medium) end,
"Comparing strings" => fn key -> bench_func(@strings_left[key], @strings_right[key], Compare.Slow) end
},
time: 10,
inputs: @inputs,
print: [fast_warning: false]
)
end
def bench_func(first, second, module) do
module.compare(first, second)
end
end
Compare.Benchmark.benchmark()