Created
February 7, 2015 14:59
-
-
Save ccocchi/f8d58626a9ac1b9536e5 to your computer and use it in GitHub Desktop.
Profanity filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark/ips' | |
text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ullamcorper eleifend velit sed placerat. In libero leo, fringilla ut mauris nec, bibendum varius nisi. Nam rhoncus facilisis lacinia. Integer massa quam, vestibulum eget est sed, elementum vulputate nibh. Mauris fermentum tellus eu commodo blandit. Cras ornare, risus id semper lacinia, est urna maximus dolor, aliquam maximus metus sem a velit. Suspendisse vel libero eu leo volutpat eleifend accumsan nec massa. Integer eu vulputate quam, ac tristique arcu. Fusce et justo vitae nulla maximus lacinia. Integer dolor massa, finibus vel mattis a, porta eu erat. Suspendisse potenti. Sed ex magna, imperdiet sit amet augue ac, vestibulum vehicula mi. Praesent sapien neque, bibendum eget lorem fermentum, molestie semper leo.' | |
words_10 = %w(foo bar string amp baguette marine grenadine quezac bambou panda) | |
words_100 = words_10 * 10 | |
regexp_10 = Regexp.new(words_10.join('|'), Regexp::IGNORECASE) | |
regexp_100 = Regexp.new(words_100.join('|'), Regexp::IGNORECASE) | |
r_array_10 = words_10.map { |w| /\b#{w}\b/i } | |
r_array_100 = words_100.map { |w| /\b#{w}\b/i } | |
puts 'Worst case: no element found' | |
puts "Text size: #{text.size}" | |
Benchmark.ips do |x| | |
x.report('regexp_10') { (regexp_10 =~ text) != nil } | |
x.report('regexp_100') { (regexp_100 =~ text) != nil } | |
x.report('regexp_loop_10') { | |
r_array_10.each do |foul| | |
break(true) if foul =~ text | |
end | |
false | |
} | |
x.report('regexp_loop_100') { | |
r_array_100.each do |foul| | |
break(true) if foul =~ text | |
end | |
false | |
} | |
x.report('array#&_10') { (words_10 & text.split(/\W+/).map! { |w| w.downcase!; w }).empty? } | |
x.report('array#&_100') { (words_100 & text.split(/\W+/).map! { |w| w.downcase!; w }).empty? } | |
x.report('array_loop_10') { | |
text.split(/\W+/).each do |word| | |
word.downcase! | |
break(true) if words_10.include?(word) | |
end | |
false | |
} | |
x.report('array_loop_100') { | |
text.split(/\W+/).each do |word| | |
word.downcase! | |
break(true) if words_100.include?(word) | |
end | |
false | |
} | |
end | |
puts | |
words_10 = %w(integer bar string amp baguette marine grenadine quezac bambou panda) | |
words_100 = words_10 * 10 | |
regexp_10 = Regexp.new(words_10.join('|'), Regexp::IGNORECASE) | |
regexp_100 = Regexp.new(words_100.join('|'), Regexp::IGNORECASE) | |
r_array_10 = words_10.map { |w| /\b#{w}\b/i } | |
r_array_100 = words_100.map { |w| /\b#{w}\b/i } | |
puts 'Best case: first element match' | |
puts "Text size: #{text.size}" | |
Benchmark.ips do |x| | |
x.report('regexp_10') { (regexp_10 =~ text) != nil } | |
x.report('regexp_100') { (regexp_100 =~ text) != nil } | |
x.report('regexp_loop_10') { | |
r_array_10.each do |foul| | |
break(true) if foul =~ text | |
end | |
false | |
} | |
x.report('regexp_loop_100') { | |
r_array_100.each do |foul| | |
break(true) if foul =~ text | |
end | |
false | |
} | |
x.report('array#&_10') { (words_10 & text.split(/\W+/).map! { |w| w.downcase!; w }).empty? } | |
x.report('array#&_100') { (words_100 & text.split(/\W+/).map! { |w| w.downcase!; w }).empty? } | |
x.report('array_loop_10') { | |
text.split(/\W+/).each do |word| | |
word.downcase! | |
break(true) if words_10.include?(word) | |
end | |
false | |
} | |
x.report('array_loop_100') { | |
text.split(/\W+/).each do |word| | |
word.downcase! | |
break(true) if words_100.include?(word) | |
end | |
false | |
} | |
end | |
# Benchmark.compare *result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Changing
for
To be equivalent.