Skip to content

Instantly share code, notes, and snippets.

@dominikh
Created October 15, 2009 14:18
Show Gist options
  • Save dominikh/210975 to your computer and use it in GitHub Desktop.
Save dominikh/210975 to your computer and use it in GitHub Desktop.
require 'benchmark'
var = "a_string"
TIMES = 1_000_000
Benchmark.bmbm do |x|
x.report("//") do
TIMES.times { /a#{var}b/ }
end
# note: this regexp will only be built once for all iterations
x.report("//o") do
TIMES.times { /#{var}/o }
end
x.report("Regexp.new") do
TIMES.times { Regexp.new("a#{var}b") }
end
end
# Rehearsal ----------------------------------------------
# // 14.470000 0.020000 14.490000 ( 14.616450)
# //o 0.170000 0.000000 0.170000 ( 0.176770)
# Regexp.new 10.080000 0.020000 10.100000 ( 10.074714)
# ------------------------------------ total: 24.760000sec
# user system total real
# // 13.670000 0.020000 13.690000 ( 13.710231)
# //o 0.180000 0.000000 0.180000 ( 0.177539)
# Regexp.new 10.080000 0.020000 10.100000 ( 10.117770)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment