Skip to content

Instantly share code, notes, and snippets.

@iStefo
Last active July 2, 2021 07:52
Show Gist options
  • Save iStefo/d83bc2737168daf6faf7fa0d2fea2ce2 to your computer and use it in GitHub Desktop.
Save iStefo/d83bc2737168daf6faf7fa0d2fea2ce2 to your computer and use it in GitHub Desktop.
# Requires Elixir 1.12+
Mix.install([
{:benchee, "~> 1.0"}
])
# Benchmark copied from https://gist.github.com/denispeplin/99047e34b0974b00116f335651d551d6
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
# Operating System: macOS
# CPU Information: Intel(R) Core(TM) i5-8279U CPU @ 2.40GHz
# Number of Available Cores: 8
# Available memory: 16 GB
# Elixir 1.12.1
# Erlang 24.0.2
iex> So.Exclude.exclude1()
Name ips average deviation median 99th %
substract 4.88 M 205.00 ns ±1386.42% 0 ns 1000 ns
ordsets 3.03 M 330.04 ns ±10483.97% 0 ns 1000 ns
enum_filter 2.93 M 340.95 ns ±18768.28% 0 ns 1000 ns
mapset_difference 1.38 M 725.01 ns ±7031.83% 1000 ns 1000 ns
Comparison:
substract 4.88 M
ordsets 3.03 M - 1.61x slower +125.04 ns
enum_filter 2.93 M - 1.66x slower +135.95 ns
mapset_difference 1.38 M - 3.54x slower +520.02 ns
iex> So.Exclude.exclude2(100)
Name ips average deviation median 99th %
substract 229.89 K 4.35 μs ±433.75% 4 μs 20 μs
ordsets 215.92 K 4.63 μs ±628.66% 4 μs 22 μs
enum_filter 97.18 K 10.29 μs ±213.13% 9 μs 32 μs
mapset_difference 47.86 K 20.89 μs ±56.83% 19 μs 64 μs
Comparison:
substract 229.89 K
ordsets 215.92 K - 1.06x slower +0.28 μs
enum_filter 97.18 K - 2.37x slower +5.94 μs
mapset_difference 47.86 K - 4.80x slower +16.54 μs
iex> So.Exclude.exclude2(1_000)
Name ips average deviation median 99th %
substract 7.32 K 136.63 μs ±22.26% 124 μs 254 μs
ordsets 6.66 K 150.10 μs ±32.38% 133 μs 308 μs
mapset_difference 2.75 K 364.27 μs ±19.44% 335 μs 620 μs
enum_filter 0.98 K 1016.90 μs ±31.22% 842 μs 1871.60 μs
Comparison:
substract 7.32 K
ordsets 6.66 K - 1.10x slower +13.48 μs
mapset_difference 2.75 K - 2.67x slower +227.64 μs
enum_filter 0.98 K - 7.44x slower +880.28 μs
iex> So.Exclude.exclude2(100_000)
Name ips average deviation median 99th %
ordsets 24.55 40.74 ms ±24.12% 37.26 ms 75.39 ms
substract 24.10 41.50 ms ±4.97% 41.50 ms 46.66 ms
mapset_difference 17.36 57.60 ms ±4.80% 57.72 ms 65.84 ms
enum_filter 0.0775 12909.30 ms ±0.00% 12909.30 ms 12909.30 ms
Comparison:
ordsets 24.55
substract 24.10 - 1.02x slower +0.76 ms
mapset_difference 17.36 - 1.41x slower +16.86 ms
enum_filter 0.0775 - 316.89x slower +12868.56 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment