Last active
June 13, 2016 09:54
-
-
Save rochefort/63f2a90ffa0f6c38e51d03a286f5f182 to your computer and use it in GitHub Desktop.
regexp is slow?
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' | |
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 |
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
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