Fastest way to check if a string matches a regexp in ruby?

前端 未结 7 1464
独厮守ぢ
独厮守ぢ 2020-12-13 05:07

What is the fastest way to check if a string matches a regular expression in Ruby?

My problem is that I have to \"egrep\" through a huge list of strings to find whic

7条回答
  •  囚心锁ツ
    2020-12-13 05:45

    What about re === str (case compare)?

    Since it evaluates to true or false and has no need for storing matches, returning match index and that stuff, I wonder if it would be an even faster way of matching than =~.


    Ok, I tested this. =~ is still faster, even if you have multiple capture groups, however it is faster than the other options.

    BTW, what good is freeze? I couldn't measure any performance boost from it.

提交回复
热议问题