Finding if a sentence contains a specific phrase in Ruby

后端 未结 4 1963
不思量自难忘°
不思量自难忘° 2020-12-10 00:06

Right now I am seeing if a sentence contains a specific word by splitting the sentence into an array and then doing an include to see if it contains the word. Something lik

4条回答
  •  情歌与酒
    2020-12-10 00:32

    Here's a non-answer showing the benchmark for the code by @TheTinMan for Ruby 1.9.2 on OS X. Note the difference in relative performance, specifically the improvements in the 2nd and 3rd tests.

                                   user     system      total        real
    array search:              7.960000   0.000000   7.960000 (  7.962338)
    literal search:            0.450000   0.010000   0.460000 (  0.445905)
    string include?:           0.400000   0.000000   0.400000 (  0.400932)
    regex:                     0.510000   0.000000   0.510000 (  0.512635)
    wildcard regex:            0.520000   0.000000   0.520000 (  0.514800)
    anchored regex:            0.510000   0.000000   0.510000 (  0.513328)
    anchored wildcard regex:   0.520000   0.000000   0.520000 (  0.517759)
    /regex/.match              0.940000   0.000000   0.940000 (  0.943471)
    /regex$/.match             0.940000   0.000000   0.940000 (  0.936782)
    /regex/ =~                 0.440000   0.000000   0.440000 (  0.446921)
    /regex$/ =~                0.450000   0.000000   0.450000 (  0.447904)
    

    I ran these results with Benchmark.bmbm, but the results do not differ between the rehearsal round and the actual timings, shown above.

提交回复
热议问题