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

Mar*_*rry 5 ruby regex ruby-on-rails-3

我正在使用Rails 3.2.3和Ruby 1.9.3p0.

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

<% if ['todo','pending','history'].include?(params[:category]) %>
Run Code Online (Sandbox Code Playgroud)

或正则表达式equals-tilde匹配速记与垂直条分隔选项:

<% if params[:category] =~ /todo|pending|history/ %>
Run Code Online (Sandbox Code Playgroud)

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

还有更好的方法吗?

ms-*_*-tg 9

总结:Array#include?String元件赢得了既接受和拒绝输入,只有三个可接受的值的例子.对于更大的一组检查,它看起来像Set#include?String元素可能取胜.


如何测试

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

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

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

这是一个经验测试脚本:

require 'benchmark'
require 'set'

strings = ['todo','pending','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?, Strings, 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?, Symbols, rejected:") { rejected_inputs.map {|s| symbols.include?(s.to_sym) } }
  x.report("Set#include?, Strings, accepted:") { accepted_inputs.map {|s| string_set.include?(s) } }
  x.report("Set#include?, Strings, 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, interpreted, 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, compiled, rejected:") { rejected_inputs.map {|s| regex_compiled.match(s) } }
end
Run Code Online (Sandbox Code Playgroud)

结果

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

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

结论

(见上面的摘要)

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

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

  • @MarkBerry感谢恭维.我喜欢Ruby让回答这些问题变得如此简单.我同意,对于一小部分允许的选项,它会在你的表现中产生噪音,所以要使用看起来最好的选项.但是,如果你有很多允许的字符串,你最终需要使用Set #include来提高性能,我很确定. (2认同)