【问题标题】:What is the fastest way to compare the start or end of a String with a sub-string using Ruby?使用 Ruby 将字符串的开头或结尾与子字符串进行比较的最快方法是什么?
【发布时间】:2015-10-06 18:10:57
【问题描述】:

对字符串进行切片"Hello world!"[0, 5] == 'Hello' 是Ruby 中的一个常用习惯用法,用于将字符串的第一个或最后n 个字符与另一个字符串进行比较。正则表达式也可以做到。还有start_with?end_with? 也可以做到这一点。

我应该使用哪个速度最快?

【问题讨论】:

    标签: ruby regex string


    【解决方案1】:

    考虑这些测试:

    require 'fruity'
    
    STR = '!' + ('a'..'z').to_a.join # => "!abcdefghijklmnopqrstuvwxyz"
    

    单个字符开始字符串的结果:

    compare do
      _slice { STR[0] == '!' }
      _start_with { STR.start_with?('!') }
      _regex { !!STR[/^!/] }
    end
    
    # >> Running each test 32768 times. Test will take about 1 second.
    # >> _start_with is faster than _slice by 2x ± 1.0
    # >> _slice is similar to _regex
    

    多个字符开始一个字符串的结果:

    compare do
      _slice { STR[0..4] == '!abcd' }
      _start_with { STR.start_with?('!abcd') }
      _regex { !!STR[/^!abcd/] }
    end
    
    # >> Running each test 32768 times. Test will take about 2 seconds.
    # >> _start_with is faster than _slice by 2x ± 1.0
    # >> _slice is similar to _regex
    

    单个字符结束字符串的结果:

    compare do
      _slice { STR[-1] == 'z' }
      _end_with { STR.end_with?('z') }
      _regex { !!STR[/z$/] }
    end
    
    # >> Running each test 32768 times. Test will take about 2 seconds.
    # >> _end_with is faster than _slice by 2x ± 1.0
    # >> _slice is faster than _regex by 2x ± 1.0
    

    多个字符结束一个字符串的结果:

    compare do
      _slice { STR[-5..-1] == 'vwxyz' }
      _end_with { STR.end_with?('vwxyz') }
      _regex { !!STR[/vwxyz$/] }
    end
    
    # >> Running each test 16384 times. Test will take about 1 second.
    # >> _end_with is faster than _slice by 2x ± 1.0
    # >> _slice is similar to _regex
    

    因此,为了清晰和快速,start_with?end_with? 应该是我们的首选。如果我们需要使用模式,那么切片或使用正则表达式是显而易见的选择。

    【讨论】:

    • 对此进行基准测试是有道理的。期待 start_with 吗?和 end_with?更快,因为他们可能不会构建任何字符串并将其返回给您,并且可以对字符串本身进行比较。
    • start_with?end_with? 快速的原因是因为它们已编译并使用memcmp(),而不是解释并且不依赖于调用正则表达式引擎。换句话说,它们依赖于用于比较字符/字节的 C 函数,这非常快。
    • 我相信我们同意:)(即说同样的话)
    猜你喜欢
    • 2014-09-03
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    • 2017-02-20
    • 1970-01-01
    相关资源
    最近更新 更多