Skip to content

Instantly share code, notes, and snippets.

@koudelka koudelka/lookup.ex
Created Oct 2, 2015

What would you like to do?
Elixir Set Lookup
defmodule Lookup do
@wordfile "words.txt"
@external_resource @wordfile
@times 1_000_000
@words @wordfile |>! |>
@hash_set Enum.into(@words,
@map_set Enum.into(@words,
def time({f, a}, expected) do
Enum.each(0..@times, fn _ ->
^expected = apply(__MODULE__, f, [a])
{_, 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"
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
def member?(_), do: false
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)

This comment has been minimized.

Copy link

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...
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
You can’t perform that action at this time.