Open
Description
Environment
jruby 9.2.1.0-SNAPSHOT (2.5.0) 2018-07-20 a2bc9a3 Java HotSpot(TM) 64-Bit Server VM 9.0.4+11 on 9.0.4+11 +jit [linux-x86_64]
ruby 2.6.0dev (2018-07-20 trunk 64004) [x86_64-linux]
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]
require 'benchmark/ips'
pattern = /^([0-1]?[0-9]|2[0-3])([:\-,\.])?([0-5][0-9])$/
string = '9:30'
Benchmark.ips do |x|
x.report('match') { string.match(pattern) }
x.report('match?') { string.match?(pattern) }
end
ruby 2.5.0
match 926.635k (± 2.6%) i/s - 4.634M in 5.004093s
match? 3.395M (± 5.5%) i/s - 16.945M in 5.013444s
ruby master
match 1.024M (± 4.4%) i/s - 5.123M in 5.013162s
match? 4.809M (± 6.9%) i/s - 23.905M in 5.001929s
ruby master + jit
match 984.690k (± 7.8%) i/s - 4.911M in 5.018228s
match? 5.348M (±11.2%) i/s - 26.410M in 5.009641s
jruby master
match 1.158M (± 9.2%) i/s - 5.712M in 4.995932s
match? 1.222M (± 5.6%) i/s - 6.092M in 5.005920s
jruby master + indy
match 1.211M (±11.0%) i/s - 5.912M in 4.985816s
match? 1.362M (± 4.1%) i/s - 6.782M in 4.989386s
match? should perform better https://bugs.ruby-lang.org/issues/8110
but jruby implement it just like match, without setting a back-reference on a ruby level
ruby has a specialized method for it and it looks like there has been some improvements recently
https://github.com/ruby/ruby/blob/trunk/re.c#L3324
I found match? pattern very useful on many places, so I think it's worth investigating if there's a way to improve it.