Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
defmodule Bench do
@sample 1000
defp average(time) do
time / @sample
end
defp bench(fun) do
f = fn ->
Enum.each 1..@sample, fn _ -> fun.() end
end
pid = spawn_link(fn ->
:timer.tc(f, []) |> elem(0) |> average |> IO.inspect
end)
receive do: ({ :EXIT, ^pid, :normal } -> :ok)
end
def run do
large_list = Enum.to_list(1..10000)
large_set = HashSet.new(large_list)
erl_large_set = :sets.from_list(large_list)
IO.puts "HashSet.new"
bench fn -> HashSet.new(large_list) end
IO.puts ":sets.from_list"
bench fn -> :sets.from_list(large_list) end
IO.puts ""
large_list = Enum.to_list(1..10000)
large_set = HashSet.new(large_list)
erl_large_set = :sets.from_list(large_list)
IO.puts "HashSet.member?"
bench fn -> HashSet.member?(large_set, 100000000) end
IO.puts ":sets.is_element"
bench fn -> :sets.is_element(100000000, erl_large_set) end
IO.puts ""
large_list = Enum.to_list(1..10000)
large_set = HashSet.new(large_list)
erl_large_set = :sets.from_list(large_list)
IO.puts "HashSet.intersection?"
bench fn -> HashSet.intersection(large_set, large_set) end
IO.puts ":sets.intersection"
bench fn -> :sets.intersection(erl_large_set, erl_large_set) end
IO.puts ""
large_list = Enum.to_list(1..10000)
large_set = HashSet.new(large_list)
erl_large_set = :sets.from_list(large_list)
IO.puts "HashSet.union"
bench fn -> HashSet.union(large_set, large_set) end
IO.puts ":sets.union"
bench fn -> :sets.union(erl_large_set, erl_large_set) end
IO.puts ""
large_list = Enum.to_list(1..10000)
large_set = HashSet.new(large_list)
erl_large_set = :sets.from_list(large_list)
IO.puts "HashSet.difference"
bench fn -> HashSet.difference(large_set, large_set) end
IO.puts ":sets.substract"
bench fn -> :sets.subtract(erl_large_set, erl_large_set) end
IO.puts ""
large_list = Enum.to_list(1..10000)
large_set = HashSet.new(large_list)
erl_large_set = :sets.from_list(large_list)
IO.puts "HashSet.size"
bench fn -> HashSet.size(large_set) end
IO.puts ":sets.size"
bench fn -> :sets.size(erl_large_set) end
IO.puts ""
large_list = Enum.to_list(1..10000)
large_set = HashSet.new(large_list)
erl_large_set = :sets.from_list(large_list)
IO.puts "HashSet.to_list"
bench fn -> HashSet.to_list(large_set) end
IO.puts ":sets.to_list"
bench fn -> :sets.to_list(erl_large_set) end
IO.puts ""
large_list = Enum.to_list(1..9999)
large_set = HashSet.new(large_list)
erl_large_set = :sets.from_list(large_list)
IO.puts "HashSet.put"
bench fn -> HashSet.put(large_set, 999999999) end
IO.puts ":sets.add_element"
bench fn -> :sets.add_element(999999999, erl_large_set) end
IO.puts ""
large_list = Enum.to_list(1..10000)
large_set = HashSet.new(large_list)
erl_large_set = :sets.from_list(large_list)
IO.puts "HashSet.delete"
bench fn -> HashSet.delete(large_set, 100000000) end
IO.puts ":sets.del_element"
bench fn -> :sets.del_element(100000000, erl_large_set) end
IO.puts ""
large_list = Enum.to_list(1..10000)
large_set = HashSet.new(large_list)
erl_large_set = :sets.from_list(large_list)
IO.puts "HashSet.subset?"
bench fn -> HashSet.subset?(large_set, large_set) end
IO.puts ":sets.is_subset"
bench fn -> :sets.is_subset(erl_large_set, erl_large_set) end
IO.puts ""
large_list = Enum.to_list(1..10000)
large_set = HashSet.new(large_list)
erl_large_set = :sets.from_list(large_list)
IO.puts "HashSet.disjoint?"
bench fn -> HashSet.disjoint?(large_set, large_set) end
IO.puts ":sets.is_disjoint"
bench fn -> :sets.is_disjoint(erl_large_set, erl_large_set) end
IO.puts ""
end
end
Bench.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment