我刚刚阅读了有关Regexp.match?('string')
Ruby 2.4 的信息,很高兴看到结果!但是当我在我的应用程序中尝试它时,我几乎没有看到任何收获。
str = 's'
Benchmark.bm do |b|
b.report(".match ") { 100000.times { 'string'.match /s/ } }
b.report(".match? ") { 100000.times { 'string'.match? /s/ } }
b.report(".match dynamic ") { 100000.times { 'string'.match /#{str}/ } }
b.report(".match? dynamic") { 100000.times { 'string'.match? /#{str}/ } }
end
user system total real
.match 0.140000 0.000000 0.140000 ( 0.143658)
.match? 0.020000 0.000000 0.020000 ( 0.029628)
.match dynamic 0.370000 0.010000 0.380000 ( 0.371935)
.match? dynamic 0.260000 0.010000 0.270000 ( 0.278614)
.match
从基准测试中,我们看到了to的巨大收益.match?
,但是一旦我开始根据应用程序的需要动态创建复杂的正则表达式,我就会失去很多收益。
我的问题是,为什么会有如此巨大的差异,我可以以某种方式创建动态正则表达式来利用.matches?
下面示例中的性能吗?我使用ruby 2.4.2p198
str = 'my text with words'
reg_str = '((^|[\s\"“])(cherry pie|cherry pies)($|[\s\"”\.\,\:\?\!])|(\#(cherrypie|cherrypies)($|\s|\#|\.|\,|\:|\?|\!)))'
puts Benchmark.measure {
100000.times { str.match? /#{reg_str}/i }
}
9.380000 0.010000 9.390000 ( 9.403821)
puts Benchmark.measure {
100000.times { str.match? /((^|[\s\"“])(cherry pie|cherry pies)($|[\s\"”\.\,\:\?\!])|(\#(cherrypie|cherrypies)($|\s|\#|\.|\,|\:|\?|\!)))/i }
}
0.020000 0.000000 0.020000 ( 0.017900)