Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mix.install([
{:benchee, "~> 1.0"},
{:benchee_html, "~> 1.0", only: :dev}
])
minimal_size = 5
medium_size = 1000
big_size = 10_000
build_list = fn size ->
Enum.map(1..size, fn _ -> %{attr: Enum.random(1..200_000)} end)
end
Benchee.run(
%{
"filter_any?" => fn %{a: list_a, b: list_b} ->
Enum.filter(list_a, fn el_a ->
Enum.any?(list_b, &Kernel.==(&1.attr, el_a.attr))
end)
end,
"using_in" => fn %{a: list_a, b: list_b} ->
list_b_attrs = list_b |> Enum.map(& &1.attr) |> Enum.uniq()
Enum.filter(list_a, fn el_a ->
el_a.attr in list_b_attrs
end)
end
},
inputs: %{
"minimal A, minimal B" => %{a: build_list.(minimal_size), b: build_list.(minimal_size)},
"medium A, minimal B" => %{a: build_list.(medium_size), b: build_list.(minimal_size)},
"big A, minimal B" => %{a: build_list.(big_size), b: build_list.(minimal_size)},
"minimal A, medium B" => %{a: build_list.(minimal_size), b: build_list.(medium_size)},
"medium A, medium B" => %{a: build_list.(medium_size), b: build_list.(medium_size)},
"big A, medium B" => %{a: build_list.(big_size), b: build_list.(medium_size)},
"minimal A, big B" => %{a: build_list.(minimal_size), b: build_list.(big_size)},
"medium A, big B" => %{a: build_list.(medium_size), b: build_list.(big_size)},
"big A, big B" => %{a: build_list.(big_size), b: build_list.(big_size)}
},
time: 10,
formatters: [
Benchee.Formatters.HTML,
Benchee.Formatters.Console
]
)
##### With input big A, big B #####
Name ips average deviation median 99th %
using_in 9.50 0.105 s ±8.80% 0.102 s 0.142 s
filter_any? 0.51 1.96 s ±2.25% 1.96 s 2.01 s
Comparison:
using_in 9.50
filter_any? 0.51 - 18.62x slower +1.85 s
##### With input big A, medium B #####
Name ips average deviation median 99th %
using_in 96.01 10.42 ms ±6.06% 10.29 ms 13.45 ms
filter_any? 5.07 197.17 ms ±2.35% 196.54 ms 209.70 ms
Comparison:
using_in 96.01
filter_any? 5.07 - 18.93x slower +186.76 ms
##### With input big A, minimal B #####
Name ips average deviation median 99th %
using_in 3.94 K 0.25 ms ±14.32% 0.25 ms 0.46 ms
filter_any? 0.74 K 1.36 ms ±10.15% 1.34 ms 1.96 ms
Comparison:
using_in 3.94 K
filter_any? 0.74 K - 5.36x slower +1.11 ms
##### With input medium A, big B #####
Name ips average deviation median 99th %
using_in 71.99 13.89 ms ±12.98% 14.66 ms 16.84 ms
filter_any? 5.07 197.41 ms ±5.08% 195.10 ms 255.83 ms
Comparison:
using_in 71.99
filter_any? 5.07 - 14.21x slower +183.52 ms
##### With input medium A, medium B #####
Name ips average deviation median 99th %
using_in 876.37 1.14 ms ±9.68% 1.12 ms 1.76 ms
filter_any? 50.94 19.63 ms ±2.85% 19.58 ms 21.87 ms
Comparison:
using_in 876.37
filter_any? 50.94 - 17.20x slower +18.49 ms
##### With input medium A, minimal B #####
Name ips average deviation median 99th %
using_in 38.76 K 25.80 μs ±57.78% 25 μs 53 μs
filter_any? 6.87 K 145.59 μs ±76.39% 130 μs 316 μs
Comparison:
using_in 38.76 K
filter_any? 6.87 K - 5.64x slower +119.79 μs
##### With input minimal A, big B #####
Name ips average deviation median 99th %
filter_any? 1.00 K 1.00 ms ±11.63% 0.97 ms 1.59 ms
using_in 0.48 K 2.10 ms ±21.61% 2 ms 4.63 ms
Comparison:
filter_any? 1.00 K
using_in 0.48 K - 2.11x slower +1.10 ms
##### With input minimal A, medium B #####
Name ips average deviation median 99th %
filter_any? 9.99 K 100.14 μs ±16.38% 98 μs 185 μs
using_in 7.87 K 127.09 μs ±16.22% 122 μs 234 μs
Comparison:
filter_any? 9.99 K
using_in 7.87 K - 1.27x slower +26.94 μs
##### With input minimal A, minimal B #####
Name ips average deviation median 99th %
using_in 1.39 M 718.97 ns ±5865.12% 1000 ns 1000 ns
filter_any? 1.10 M 912.17 ns ±4369.28% 1000 ns 1000 ns
Comparison:
using_in 1.39 M
filter_any? 1.10 M - 1.27x slower +193.20 ns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment