Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env ruby -wKU

require "benchmark"

TESTS = 1_000_000
STR   = "A" * 1000 + "BC"
RE    = /ABC/
Benchmark.bmbm do |results|
  results.report("=~:")    { TESTS.times { STR =~ RE     } }
  results.report("[]:")    { TESTS.times { STR[RE]       } }
  results.report("match:") { TESTS.times { STR.match(RE) } }
end
# >> Rehearsal ------------------------------------------
# >> =~:      5.900000   0.340000   6.240000 (  6.246189)
# >> []:      6.400000   0.370000   6.770000 (  6.763429)
# >> match:   6.880000   0.370000   7.250000 (  7.258122)
# >> -------------------------------- total: 20.260000sec
# >> 
# >>              user     system      total        real
# >> =~:      5.880000   0.340000   6.220000 (  6.227077)
# >> []:      6.400000   0.360000   6.760000 (  6.759785)
# >> match:   6.880000   0.370000   7.250000 (  7.252039)