Skip to content

Instantly share code, notes, and snippets.

@denispeplin
Last active February 18, 2021 19:55
Show Gist options
  • Save denispeplin/99047e34b0974b00116f335651d551d6 to your computer and use it in GitHub Desktop.
Save denispeplin/99047e34b0974b00116f335651d551d6 to your computer and use it in GitHub Desktop.
iex> So.Exclude.exclude1
Name ips average deviation median
substract 9.18 M 0.109 μs ±358.78% 0.100 μs
mapset_difference 4.00 M 0.25 μs ±17781.86% 0.0 μs
enum_filter 3.70 M 0.27 μs ±1712.25% 0.20 μs
ordsets 2.63 M 0.38 μs ±1348.35% 0.30 μs
Comparison:
substract 9.18 M
mapset_difference 4.00 M - 2.30x slower
enum_filter 3.70 M - 2.48x slower
ordsets 2.63 M - 3.49x slower
iex> So.Exclude.exclude2(100)
Name ips average deviation median
enum_filter 69.86 K 14.31 μs ±68.81% 14.00 μs
ordsets 46.50 K 21.51 μs ±73.18% 20.00 μs
mapset_difference 35.97 K 27.80 μs ±16.81% 27.00 μs
substract 31.04 K 32.22 μs ±32.36% 31.00 μs
Comparison:
enum_filter 69.86 K
ordsets 46.50 K - 1.50x slower
mapset_difference 35.97 K - 1.94x slower
substract 31.04 K - 2.25x slower
iex> So.Exclude.exclude2(1000)
Name ips average deviation median
ordsets 2.88 K 0.35 ms ±6.36% 0.34 ms
mapset_difference 2.06 K 0.49 ms ±15.54% 0.45 ms
enum_filter 0.94 K 1.07 ms ±20.62% 0.93 ms
substract 0.31 K 3.24 ms ±14.54% 3.04 ms
Comparison:
ordsets 2.88 K
mapset_difference 2.06 K - 1.40x slower
enum_filter 0.94 K - 3.08x slower
substract 0.31 K - 9.34x slower
iex> So.Exclude.exclude2(100000)
Name ips average deviation median
ordsets 13.46 0.0743 s ±9.42% 0.0724 s
mapset_difference 12.81 0.0781 s ±6.77% 0.0763 s
enum_filter 0.0581 17.21 s ±0.00% 17.21 s
substract 0.0220 45.41 s ±0.00% 45.41 s
Comparison:
ordsets 13.46
mapset_difference 12.81 - 1.05x slower
enum_filter 0.0581 - 231.67x slower
substract 0.0220 - 611.30x slower
defmodule So.Exclude do
def exclude1() do
a = [1,2,3,4]
b = [3,4,5,6]
run_benchmarks(a, b)
end
def exclude2(size, max_value \\ nil) do
max_value = max_value || size
a = random_list(size, max_value)
b = random_list(size, max_value)
run_benchmarks(a, b)
end
defp run_benchmarks(a, b) do
Benchee.run(%{
"enum_filter" => fn ->
Enum.filter(a, fn el -> !Enum.member?(b, el) end)
end,
"substract" => fn ->
a -- b
end,
"mapset_difference" => fn ->
MapSet.difference(MapSet.new(a), MapSet.new(b)) |> MapSet.to_list
end,
"ordsets" => fn ->
:ordsets.subtract(:ordsets.from_list(a), :ordsets.from_list(b)) |> :ordsets.to_list
end
}, time: 10)
end
defp random_list(size, max_value) do
Enum.map(1..size, fn(_size) -> Enum.random(1..max_value) end)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment