Skip to content

Instantly share code, notes, and snippets.

@mthssdrbrg
Last active December 18, 2015 07:09
Show Gist options
  • Save mthssdrbrg/5744825 to your computer and use it in GitHub Desktop.
Save mthssdrbrg/5744825 to your computer and use it in GitHub Desktop.
require 'benchmark'
iterations = 1_000_000
bm_string = 'parenthesize this'
Benchmark.bmbm(7) do |x|
x.report("gsub:") do
iterations.times { bm_string.gsub(/^(.+)$/, '(\1)') }
end
x.report("gsub!:") do
iterations.times { bm_string.dup.gsub!(/^(.+)$/, '(\1)') }
end
x.report("surround:") do
iterations.times { '(' + bm_string + ')' }
end
x.report("interpolation:") do
iterations.times { "(#{bm_string})" }
end
x.report("percent:") do
iterations.times { '(%s)' % bm_string }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment