ruby - Ruby 2.4:如何加速正则表达式动态初始化以与 .match 一起使用?
问题描述
我刚刚阅读了有关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)
解决方案
使用/o修饰符,因此插值只执行一次:
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}/o } }
b.report(".match? dynamic") { 100000.times { 'string'.match? /#{str}/o } }
end
user system total real
.match 0.120000 0.010000 0.130000 ( 0.117889)
.match? 0.020000 0.000000 0.020000 ( 0.027255)
.match dynamic 0.110000 0.000000 0.110000 ( 0.113300)
.match? dynamic 0.030000 0.000000 0.030000 ( 0.034755)
推荐阅读
- javascript - IBM Cognos Analytics 与 D3.js 条形图高度问题
- php - C:/Apache24/conf/httpd.conf: Cannot load c:/php7/php7apache2.dll into server: 找不到指定的模块
- java - 如何生成密钥以获得 HMAC SHA512 的 TOTP 符合 RFC6238 和 RFC4086?
- gradle - Gradle 任务以创建 zip 存档
- php - 使用 woocommerce_add_to_cart_validation 挂钩时禁用页面重新加载
- javascript - 将 Mongoose/Multer 文件上传的工作模式/FORM 更改为 MULTIPART/FORM - 模式保持打开
- python - 如何设置 Python IDE 使自动完成工作?
- jquery - JSON 中有意外的令牌 j,但没有令牌 j
- javascript - 带有 Sweet Alert 的 Laravel 删除功能
- vb.net - '只读数据已修改'