Skip to content

Instantly share code, notes, and snippets.

@rochefort
Last active June 13, 2016 09:54
Show Gist options
  • Save rochefort/63f2a90ffa0f6c38e51d03a286f5f182 to your computer and use it in GitHub Desktop.
Save rochefort/63f2a90ffa0f6c38e51d03a286f5f182 to your computer and use it in GitHub Desktop.
regexp is slow?
require 'benchmark/ips'
str = "aaabbbccc"
ar = ['aaa', 'ccc']
Benchmark.ips do |x|
x.report('String#include?') do
str.include?('aaa') or str.include?('ccc')
end
x.report('Array#include?') do
ar.any? { |w| str.include? w }
end
x.report('match') do
str.match(/aaa|ccc/)
end
end
Warming up --------------------------------------
String#include? 223.498k i/100ms
Array#include? 199.110k i/100ms
match 71.600k i/100ms
Calculating -------------------------------------
String#include? 5.195M (± 4.5%) i/s - 26.149M in 5.044216s
Array#include? 4.171M (± 3.5%) i/s - 20.907M in 5.018528s
match 902.854k (± 3.2%) i/s - 4.511M in 5.001141s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment