vadviktor
9/26/2015 - 8:47 PM

Ruby regexp text matching

Ruby regexp text matching

Calculating -------------------------------------
               match   103.178k i/100ms
                  =~   112.805k i/100ms
                  []   102.656k i/100ms
-------------------------------------------------
               match      2.572M (±11.7%) i/s -     12.691M
                  =~      3.175M (± 6.9%) i/s -     15.793M
                  []      2.996M (±10.4%) i/s -     14.782M

Comparison:
                  =~:  3175088.4 i/s
                  []:  2995669.0 i/s - 1.06x slower
               match:  2571572.8 i/s - 1.23x slower
require 'benchmark/ips'

string = "foo".freeze

Benchmark.ips do |x|
  x.report("match") {
    /boo/.match(string)
  }
  x.report("=~") {
    /boo/ =~ string
  }
  x.report("[]") {
    string[/boo/]
  }

  x.compare!
end