Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Elixir Set Lookup
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)
@rrrene

This comment has been minimized.

Show comment Hide comment
@rrrene

rrrene Oct 3, 2015

Hi Michael,

do these numbers match yours?

$ wc -l words.txt 

21110 words.txt

$ elixir lookup.exs 

Compiling function clauses, this'll take a second...
...done
hash_set_member? took 130000µs to look up 'zymase' 1000000 times, 0.13 µs/lookup
hash_set_member? took 350000µs to look up 'an invalid word' 1000000 times, 0.35 µs/lookup
map_set_member? took 159480000µs to look up 'zymase' 1000000 times, 159.48 µs/lookup
map_set_member? took 120330000µs to look up 'an invalid word' 1000000 times, 120.33 µs/lookup
member? took 240000µs to look up 'zymase' 1000000 times, 0.24 µs/lookup
member? took 200000µs to look up 'an invalid word' 1000000 times, 0.2 µs/lookup

rrrene commented Oct 3, 2015

Hi Michael,

do these numbers match yours?

$ wc -l words.txt 

21110 words.txt

$ elixir lookup.exs 

Compiling function clauses, this'll take a second...
...done
hash_set_member? took 130000µs to look up 'zymase' 1000000 times, 0.13 µs/lookup
hash_set_member? took 350000µs to look up 'an invalid word' 1000000 times, 0.35 µs/lookup
map_set_member? took 159480000µs to look up 'zymase' 1000000 times, 159.48 µs/lookup
map_set_member? took 120330000µs to look up 'an invalid word' 1000000 times, 120.33 µs/lookup
member? took 240000µs to look up 'zymase' 1000000 times, 0.24 µs/lookup
member? took 200000µs to look up 'an invalid word' 1000000 times, 0.2 µs/lookup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment