Skip to content

Instantly share code, notes, and snippets.

@outoftime
Created August 31, 2009 20:57
Show Gist options
  • Save outoftime/178707 to your computer and use it in GitHub Desktop.
Save outoftime/178707 to your computer and use it in GitHub Desktop.
require 'benchmark'
PATTERN = /@@@(.*?)@@@/
TIMES = 1_000
string = 'one @@@two@@@ three' * 1000
Benchmark.bm(10) do |benchmark|
benchmark.report('global') do
TIMES.times do
string.gsub(PATTERN) { Regexp.last_match(1).reverse }
end
end
benchmark.report('local') do
TIMES.times do
string.gsub(PATTERN) { |match| PATTERN.match(match)[1].reverse }
end
end
end
user system total real
global 2.530000 0.000000 2.530000 ( 2.604000)
local 4.150000 0.020000 4.170000 ( 4.225362)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment