Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 检查两个字符串或RegExp是否完全匹配时,哪种方法更快?_Ruby_String - Fatal编程技术网

Ruby 检查两个字符串或RegExp是否完全匹配时,哪种方法更快?

Ruby 检查两个字符串或RegExp是否完全匹配时,哪种方法更快?,ruby,string,Ruby,String,在检查两个字符串或RegExp是否完全匹配时,哪种方法(字符级)更快?如果字符串非常长或需要多次检查 str==str_或_regexp | | str=~str_或_regexp str[str\u或\u regexp]==str 还是有更好的办法 直到运行时,我们才知道str\u或\u regexp是字符串或正则表达式。我更喜欢string.match(pattern)我自己,这似乎是建议的方法。以下是它的用法。我更喜欢string.match(pattern)我自己,这似乎是建议的方法。以

在检查两个字符串或RegExp是否完全匹配时,哪种方法(字符级)更快?如果字符串非常长或需要多次检查

  • str==str_或_regexp | | str=~str_或_regexp

  • str[str\u或\u regexp]==str

  • 还是有更好的办法


    直到运行时,我们才知道
    str\u或\u regexp
    是字符串或正则表达式。

    我更喜欢
    string.match(pattern)
    我自己,这似乎是建议的方法。以下是它的用法。

    我更喜欢
    string.match(pattern)
    我自己,这似乎是建议的方法。以下是解决此问题的方法。

    当所有其他方法都失败时,运行一些测试:

    str = 'string'
    s = 'string'
    r = /string/
    
    methods = {
      :equals_or_matches => lambda { |t| str == t || str =~ t },
      :square_brackets => lambda { |t| str[t] == str }
    }
    
    methods.each_pair do |name, method|
      puts name
    
      [s, r].each do |t|
        puts t.class
    
        5.times do
          start = Time.now
          1000000.times do
            method.call(t)
          end
          puts Time.now - start
        end
      end
    
      puts
    end
    
    我得到了这些结果:

    equals_or_matches
    String
    0.942799
    0.942405
    0.944376
    0.946296
    0.93843
    Regexp
    1.916263
    1.915058
    1.913306
    1.934423
    1.932633
    
    square_brackets
    String
    1.15087
    1.157245
    1.157863
    1.174356
    1.188758
    Regexp
    2.09721
    2.103493
    2.028035
    2.025194
    2.037734
    
    equals_or_matches
    String
    0.936063
    0.94154
    0.938561
    0.934187
    0.935868
    Regexp
    2.755815
    2.75011
    2.758374
    2.761684
    2.76826
    
    square_brackets
    String
    1.198433
    1.160929
    1.354407
    1.410265
    1.274158
    Regexp
    2.013017
    2.275579
    2.297108
    2.165399
    2.125889
    
    这表明第一种方法比第二种方法快一点

    但是,如果字符串不相等,并且
    |
    没有“短路”,则会出现
    类型错误。您不能将字符串传递给
    =~
    。因此,您可能应该将其替换为
    str.match(t)
    ,这给了我以下结果:

    equals_or_matches
    String
    0.942799
    0.942405
    0.944376
    0.946296
    0.93843
    Regexp
    1.916263
    1.915058
    1.913306
    1.934423
    1.932633
    
    square_brackets
    String
    1.15087
    1.157245
    1.157863
    1.174356
    1.188758
    Regexp
    2.09721
    2.103493
    2.028035
    2.025194
    2.037734
    
    equals_or_matches
    String
    0.936063
    0.94154
    0.938561
    0.934187
    0.935868
    Regexp
    2.755815
    2.75011
    2.758374
    2.761684
    2.76826
    
    square_brackets
    String
    1.198433
    1.160929
    1.354407
    1.410265
    1.274158
    Regexp
    2.013017
    2.275579
    2.297108
    2.165399
    2.125889
    
    在这种情况下,您的第一种方法对于regexp的效果要差得多,但第二种方法大致相同


    正如我所说,只需对真实数据运行一些测试,看看会发生什么。

    当所有其他测试都失败时,运行一些测试:

    str = 'string'
    s = 'string'
    r = /string/
    
    methods = {
      :equals_or_matches => lambda { |t| str == t || str =~ t },
      :square_brackets => lambda { |t| str[t] == str }
    }
    
    methods.each_pair do |name, method|
      puts name
    
      [s, r].each do |t|
        puts t.class
    
        5.times do
          start = Time.now
          1000000.times do
            method.call(t)
          end
          puts Time.now - start
        end
      end
    
      puts
    end
    
    我得到了这些结果:

    equals_or_matches
    String
    0.942799
    0.942405
    0.944376
    0.946296
    0.93843
    Regexp
    1.916263
    1.915058
    1.913306
    1.934423
    1.932633
    
    square_brackets
    String
    1.15087
    1.157245
    1.157863
    1.174356
    1.188758
    Regexp
    2.09721
    2.103493
    2.028035
    2.025194
    2.037734
    
    equals_or_matches
    String
    0.936063
    0.94154
    0.938561
    0.934187
    0.935868
    Regexp
    2.755815
    2.75011
    2.758374
    2.761684
    2.76826
    
    square_brackets
    String
    1.198433
    1.160929
    1.354407
    1.410265
    1.274158
    Regexp
    2.013017
    2.275579
    2.297108
    2.165399
    2.125889
    
    这表明第一种方法比第二种方法快一点

    但是,如果字符串不相等,并且
    |
    没有“短路”,则会出现
    类型错误。您不能将字符串传递给
    =~
    。因此,您可能应该将其替换为
    str.match(t)
    ,这给了我以下结果:

    equals_or_matches
    String
    0.942799
    0.942405
    0.944376
    0.946296
    0.93843
    Regexp
    1.916263
    1.915058
    1.913306
    1.934423
    1.932633
    
    square_brackets
    String
    1.15087
    1.157245
    1.157863
    1.174356
    1.188758
    Regexp
    2.09721
    2.103493
    2.028035
    2.025194
    2.037734
    
    equals_or_matches
    String
    0.936063
    0.94154
    0.938561
    0.934187
    0.935868
    Regexp
    2.755815
    2.75011
    2.758374
    2.761684
    2.76826
    
    square_brackets
    String
    1.198433
    1.160929
    1.354407
    1.410265
    1.274158
    Regexp
    2.013017
    2.275579
    2.297108
    2.165399
    2.125889
    
    在这种情况下,您的第一种方法对于regexp的效果要差得多,但第二种方法大致相同


    就像我说的,只需对真实数据运行一些测试,看看会发生什么。

    你说的“完全匹配regexp”是什么意思?它要么匹配,要么不匹配。整个字符串与regexp或其他字符串匹配。就像你评论的那个,它可以部分匹配。这并不能澄清。如果regexp的模式是这样规定的,那么regexp可以只匹配字符串的一部分。那么,字符串和字符串匹配。什么是“完全匹配正则表达式”?它要么匹配,要么不匹配。整个字符串与regexp或其他字符串匹配。就像你评论的那个,它可以部分匹配。这并不能澄清。如果regexp的模式是这样规定的,那么regexp可以只匹配字符串的一部分。只有字符串匹配字符串。
    “foobar”。match(“foo”)
    返回
    true
    ,而
    “foobar”==“foo”
    不返回。如果str\u或\u regexp是字符串而不是regexp怎么办?它必须精确匹配
    “foobar”。match(“foo”)
    返回
    true
    ,而
    “foobar”==“foo”
    不返回。如果str\u或\u regexp是字符串而不是regexp怎么办?它必须精确匹配更新:我将str=~t更改为(t.is_a?(Regexp)&&str=~t),因此如果t是字符串,它将“短路”str=~t。我再次运行了您的测试,发现性能大幅提高,速度提高了50%!(我想String.match确实不是一个比较好的工具)更新:我将str=~t更改为(t.is_a?(Regexp)&&str=~t),因此如果t是字符串,它将“短路”str=~t。我再次运行了您的测试,发现性能大幅提高,速度提高了50%!(我想String.match确实不是一个好的比较对象)