检查字符串变量是否在一组字符串中

时间:2011-03-12 05:39:01

标签: ruby

哪一个更好:

x == 'abc' || x == 'def' || x == 'ghi'
%w(abc def ghi).include? x
x =~ /abc|def|ghi/

3 个答案:

答案 0 :(得分:7)

哪一个更好?这个问题不容易回答,因为他们并非都做同样的事情。

x == 'abc' || x == 'def' || x == 'ghi'
%w(abc def ghi).include? x

x与固定字符串进行比较以获得相等性。 x必须是其中一个值。在这两者之间,我倾向于选择第二种,因为它更容易维护。想象一下,如果必须与二十,五十或一百个字符串进行比较,它会是什么样子。

第三次测试:

x ~= /abc|def|ghi/

匹配子字符串:

x = 'xyzghi'
(x =~ /abc|def|ghi/) # => 3

所以它与前两个不一样。

编辑:纳什完成的基准测试中有一些事情我会采用不同的方式。在MacBook Pro上使用Ruby 1.9.2-p180,测试1,000,000个循环,并使用分组比较锚定正则表达式的结果,以及每次循环时不分割%w()数组:

require 'benchmark'
str = "test"

n = 1_000_000
Benchmark.bm do |x|
  x.report { n.times { str == 'abc' || str == 'def' || str == 'ghi' } }
  x.report { n.times { %w(abc def ghi).include? str } }
  x.report { ary = %w(abc def ghi); n.times { ary.include? str } }
  x.report { n.times { str =~ /abc|def|ghi/ } }
  x.report { n.times { str =~ /^abc|def|ghi$/ } }
  x.report { n.times { str =~ /^(abc|def|ghi)$/ } }
  x.report { n.times { str =~ /^(?:abc|def|ghi)$/ } }
  x.report { n.times { str =~ /\b(?:abc|def|ghi)\b/ } }
end
# >>       user     system      total        real
# >>   1.160000   0.000000   1.160000 (  1.165331)
# >>   1.920000   0.000000   1.920000 (  1.920120)
# >>   0.990000   0.000000   0.990000 (  0.983921)
# >>   1.070000   0.000000   1.070000 (  1.068140)
# >>   1.050000   0.010000   1.060000 (  1.054852)
# >>   1.060000   0.000000   1.060000 (  1.063909)
# >>   1.060000   0.000000   1.060000 (  1.050813)
# >>   1.050000   0.000000   1.050000 (  1.056147)

答案 1 :(得分:2)

第一个可能会更快,因为没有方法调用和你进行直接字符串比较,但它也可能是最不可读和最不易维护的。

第二个绝对是最时髦的,也是红宝石的方式。它是最易于维护的,也可能是最好的阅读。

最后一种方法使用旧式perl正则表达式语法。相当快,不像第一个维护,相当可读。

我想这取决于你所说的“更好”。

答案 2 :(得分:2)

一些基准:

require 'benchmark'
str = "test"
Benchmark.bm do |x|
  x.report {100000.times {if str == 'abc' || str == 'def' || str == 'ghi'; end}}
  x.report {100000.times {if %w(abc def ghi).include? str; end}}
  x.report {100000.times {if str =~ /abc|def|ghi/; end}}
end

    user     system      total        real
0.250000   0.000000   0.250000 (  0.251014)
0.374000   0.000000   0.374000 (  0.402023)
0.265000   0.000000   0.265000 (  0.259014)

因此,您可以看到第一种方式比其他方式更快。而且str越长,最后一种方式越慢:

str = "testasdasdasdasdasddkmfskjndfbdkjngdjgndksnfg"
    user     system      total        real
0.234000   0.000000   0.234000 (  0.248014)
0.405000   0.000000   0.405000 (  0.403023)
1.046000   0.000000   1.046000 (  1.038059)