Skip to content

Instantly share code, notes, and snippets.

@dominikh
Created October 12, 2009 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dominikh/208033 to your computer and use it in GitHub Desktop.
Save dominikh/208033 to your computer and use it in GitHub Desktop.
require 'benchmark'
TIMES = 1_000_000
string = ' some weird string with spaces in it '
Benchmark.bmbm do |x|
x.report("gsub + regexp") do
TIMES.times do |t|
string.gsub(/\s+/,'-')
end
end
x.report("gsub + string") do
TIMES.times do |t|
string.gsub(' ', '-')
end
end
x.report("tr") do
TIMES.times do |t|
string.tr(' ','-')
end
end
end
# Rehearsal -------------------------------------------------
# gsub + regexp 16.490000 0.020000 16.510000 ( 16.487952)
# gsub + string 28.230000 0.020000 28.250000 ( 28.233503)
# tr 2.760000 0.000000 2.760000 ( 2.766509)
# --------------------------------------- total: 47.520000sec
# user system total real
# gsub + regexp 16.730000 0.010000 16.740000 ( 16.737064)
# gsub + string 31.570000 0.050000 31.620000 ( 32.219622)
# tr 2.990000 0.010000 3.000000 ( 2.994367)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment