加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Ruby on Rails regexp equals-tilde与array包括用于检查选项列表

发布时间:2020-12-17 03:46:59 所属栏目:百科 来源:网络整理
导读:我正在使用Rails 3.2.3和 Ruby 1.9.3p0. 我发现我经常要确定一个字符串是否出现在选项列表中.看来我可以使用Ruby数组 .include method: % if ['todo','pending','history'].include?(params[:category]) % 或正则表达式equals-tilde match shorthand,垂直条
我正在使用Rails 3.2.3和 Ruby 1.9.3p0.

我发现我经常要确定一个字符串是否出现在选项列表中.看来我可以使用Ruby数组.include method:

<% if ['todo','pending','history'].include?(params[:category]) %>

或正则表达式equals-tilde match shorthand,垂直条分隔选项:

<% if params[:category] =~ /todo|pending|history/ %>

在性能方面,一个比另一个好吗?

还有更好的方法吗?

解决方法

摘要:数组#include?对于接受和拒绝输入的字符串元素获胜,对于您的示例只有三个可接受的值.要检查更大的集合,它看起来像Set #include?使用String元素可能会赢.

如何测试

我们应该根据经验进行测试.

以下是您可能还需要考虑的几个备选方案:预编译的正则表达式,符号列表和带有String元素的Set.

我可以想象,性能可能还取决于您的大多数输入是否属于预期集合,并且是否被接受,或者大多数输入是否在集合之外,并且被拒绝.

这是一个经验测试脚本:

require 'benchmark'
require 'set'

strings = ['todo','history']
string_set = Set.new(strings)
symbols = strings.map(&:to_sym)
regex_compiled = Regexp.new(strings.join("|"))

strings_avg_size = (strings.map(&:size).inject {|sum,n| sum + n}.to_f / strings.size).to_i
num_inputs = 1_000_000

accepted_inputs = (0...num_inputs).map { strings[rand(strings.size)] } 
rejected_inputs = (0...num_inputs).map { (0..strings_avg_size).map { ('a'...'z').to_a[rand(26)] }.join }

Benchmark.bmbm(40) do |x|
  x.report("Array#include?,Strings,accepted:") { accepted_inputs.map {|s| strings.include?(s) } }
  x.report("Array#include?,rejected:") { rejected_inputs.map {|s| strings.include?(s) } }
  x.report("Array#include?,Symbols,accepted:") { accepted_inputs.map {|s| symbols.include?(s.to_sym) } }
  x.report("Array#include?,rejected:") { rejected_inputs.map {|s| symbols.include?(s.to_sym) } }
  x.report("Set#include?,accepted:") { accepted_inputs.map {|s| string_set.include?(s) } }
  x.report("Set#include?,rejected:") { rejected_inputs.map {|s| string_set.include?(s) } }
  x.report("Regexp#match,interpreted,accepted:") { accepted_inputs.map {|s| s =~ /todo|pending|history/ } }
  x.report("Regexp#match,rejected:") { rejected_inputs.map {|s| s =~ /todo|pending|history/ } }
  x.report("Regexp#match,compiled,accepted:") { accepted_inputs.map {|s| regex_compiled.match(s) } }
  x.report("Regexp#match,rejected:") { rejected_inputs.map {|s| regex_compiled.match(s) } }
end

结果

Rehearsal ---------------------------------------------------------------------------
Array#include?,accepted:        0.210000   0.000000   0.210000 (  0.215099)
Array#include?,rejected:        0.530000   0.010000   0.540000 (  0.543898)
Array#include?,accepted:        0.330000   0.000000   0.330000 (  0.337767)
Array#include?,rejected:        1.870000   0.050000   1.920000 (  1.923155)
Set#include?,accepted:          0.270000   0.000000   0.270000 (  0.274774)
Set#include?,rejected:          0.460000   0.000000   0.460000 (  0.463925)
Regexp#match,accepted:      0.380000   0.000000   0.380000 (  0.382060)
Regexp#match,rejected:      0.650000   0.000000   0.650000 (  0.660775)
Regexp#match,accepted:         1.130000   0.080000   1.210000 (  1.220970)
Regexp#match,rejected:         0.630000   0.000000   0.630000 (  0.640721)
------------------------------------------------------------------ total: 6.600000sec

                                              user     system      total        real
Array#include?,accepted:        0.210000   0.000000   0.210000 (  0.219060)
Array#include?,rejected:        0.430000   0.000000   0.430000 (  0.444911)
Array#include?,accepted:        0.340000   0.000000   0.340000 (  0.341970)
Array#include?,rejected:        1.080000   0.000000   1.080000 (  1.089961)
Set#include?,accepted:          0.270000   0.000000   0.270000 (  0.281270)
Set#include?,rejected:          0.400000   0.000000   0.400000 (  0.406181)
Regexp#match,accepted:      0.370000   0.000000   0.370000 (  0.366931)
Regexp#match,rejected:      0.560000   0.000000   0.560000 (  0.558652)
Regexp#match,accepted:         0.920000   0.000000   0.920000 (  0.915914)
Regexp#match,rejected:         0.620000   0.000000   0.620000 (  0.627620)

结论

(见上面的摘要)

经过反思,对于我来说,符号数组对于被拒绝的输入来说非常慢是有意义的,因为在进行检查之前,必须在符号表中插入这些随机字符串中的每一个.

即使经过深思熟虑,对我来说,编译的Regexp执行得非常糟糕,特别是与在代码中被解释为文字的Regexp相比,这对我来说没有多大意义.谁能解释为什么它这么糟糕?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读