Created
October 2, 2015 21:13
-
-
Save koudelka/80a1aefc37a4019c6e64 to your computer and use it in GitHub Desktop.
Elixir Set Lookup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Lookup do | |
@wordfile "words.txt" | |
@external_resource @wordfile | |
@times 1_000_000 | |
@words @wordfile |> File.stream! |> Enum.map(&String.strip/1) | |
@hash_set Enum.into(@words, HashSet.new) | |
@map_set Enum.into(@words, MapSet.new) | |
def time({f, a}, expected) do | |
:erlang.statistics(:runtime) | |
Enum.each(0..@times, fn _ -> | |
^expected = apply(__MODULE__, f, [a]) | |
end) | |
{_, cpu_elapsed} = :erlang.statistics(:runtime) | |
cpu_elapsed_ms = cpu_elapsed * 1000 | |
IO.puts "#{f} took #{cpu_elapsed_ms}µs to look up '#{a}' #{@times} times, #{cpu_elapsed_ms / @times} µs/lookup" | |
end | |
def valid_word, do: List.last(@words) | |
def hash_set_member?(word), do: Set.member?(@hash_set, word) | |
def map_set_member?(word), do: Set.member?(@map_set, word) | |
IO.puts "Compiling function clauses, this'll take a second..." | |
Enum.each @words, fn word -> | |
def member?(unquote(word)), do: true | |
end | |
def member?(_), do: false | |
end | |
IO.puts "...done" | |
invalid_word = "an invalid word" | |
Lookup.time({:hash_set_member?, Lookup.valid_word}, true) | |
Lookup.time({:hash_set_member?, invalid_word}, false) | |
Lookup.time({:map_set_member?, Lookup.valid_word}, true) | |
Lookup.time({:map_set_member?, invalid_word}, false) | |
Lookup.time({:member?, Lookup.valid_word}, true) | |
Lookup.time({:member?, invalid_word}, false) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Michael,
do these numbers match yours?