Skip to content

Instantly share code, notes, and snippets.

@Phrogz
Created April 9, 2016 04:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Phrogz/8bd5f0eccfb90986977f1a55207fb46f to your computer and use it in GitHub Desktop.
Save Phrogz/8bd5f0eccfb90986977f1a55207fb46f to your computer and use it in GitHub Desktop.
# Benchmarking answers to http://stackoverflow.com/questions/9333952/in-ruby-how-to-make-the-string-include-method-ignore-case/9334066
# http://www.gutenberg.org/cache/epub/1661/pg1661.txt
strings = IO.read('sherlock.txt').scan(/\w+/) # 109,222 words
known = 500.times.map do
strings.sample.downcase.chars.map{ |c| rand<0.5 ? c.upcase : c }.join
end
words = known.flat_map{ |s| [s, s+"-"] } # \w does not include -
require 'set'
require 'benchmark'
Benchmark.bm(10) do |x|
x.report('regex'){
words.each do |w|
re = /\A#{w}\z/i
strings.any?{ |s| s=~re }
end
}
x.report('casecmp'){
words.each do |w|
strings.any?{ |s| s.casecmp(w)==0 }
end
}
x.report('downarray'){
words.each do |w|
strings.map(&:downcase).include?(w.downcase)
end
}
x.report('downonce'){
downed = strings.map(&:downcase)
words.each do |w|
downed.include?(w.downcase)
end
}
x.report('set_once'){
downed = Set.new strings.map(&:downcase)
words.each do |w|
downed.include?(w.downcase)
end
}
end
#=> user system total real
#=> regex 18.710000 0.020000 18.730000 ( 18.725266)
#=> casecmp 5.160000 0.000000 5.160000 ( 5.155496)
#=> downarray 16.760000 0.030000 16.790000 ( 16.809063)
#=> downonce 0.650000 0.000000 0.650000 ( 0.643165)
#=> setonce 0.040000 0.000000 0.040000 ( 0.038955)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment