Skip to content

Instantly share code, notes, and snippets.

@hrp
Created December 28, 2013 01:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrp/8155014 to your computer and use it in GitHub Desktop.
Save hrp/8155014 to your computer and use it in GitHub Desktop.
Case insensitive String#include? benchmark
marking 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 '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
}
end
#=> user system total real
#=> regex 26.770000 0.000000 26.770000 ( 26.760000)
#=> casecmp 9.375000 0.000000 9.375000 ( 9.377000)
#=> downarray 29.329000 0.000000 29.329000 ( 29.332000)
#=> downonce 3.619000 0.000000 3.619000 ( 3.620000)
@kangkyu
Copy link

kangkyu commented Feb 10, 2016

Can you change the title into Array#include? not String#include?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment