Skip to content

Instantly share code, notes, and snippets.

@sprite2005
Created June 26, 2013 00:53
Show Gist options
  • Save sprite2005/5863850 to your computer and use it in GitHub Desktop.
Save sprite2005/5863850 to your computer and use it in GitHub Desktop.
def has_word?(word)
matching_words = dictionary_words.where(:word => word.upcase).limit(1)
if matching_words.blank?
false
else
true
end
end
Slow! :(
1.9.3-p429 :069 > Benchmark.measure { d.has_word?('cracks') }
(0.6ms) SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM "dictionary_words" WHERE "dictionary_words"."dictionary_id" = 1 AND "dictionary_words"."word" = 'CRACKS' LIMIT 1) subquery_for_count
=> 0.010000 0.000000 0.010000 ( 0.003083)
Fast!
1.9.3-p429 :092 > d.dictionary_words.where(:word => 'CRACKS').limit(1)
DictionaryWord Load (0.5ms) SELECT "dictionary_words".* FROM "dictionary_words" WHERE "dictionary_words"."dictionary_id" = 1 AND "dictionary_words"."word" = 'CRACKS' LIMIT 1
=> [#<DictionaryWord id: 213357, word: "CRACKS", length: 6, dictionary_id: 1, created_at: "2013-06-24 00:29:00", updated_at: "2013-06-24 00:29:00", seed: 16382530>]
1.9.3-p429 :098 > b = Benchmark.measure { d.dictionary_words.where(:word => 'CRACKS').limit(1) }
=> 0.000000 0.000000 0.000000 ( 0.000540)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment