Skip to content

Instantly share code, notes, and snippets.

@dominikh
Created October 12, 2009 02:45
Show Gist options
  • Save dominikh/208081 to your computer and use it in GitHub Desktop.
Save dominikh/208081 to your computer and use it in GitHub Desktop.
require 'benchmark'
TIMES = 1_000_000
s = "a string"
Benchmark.bmbm do |x|
x.report("delete") do
TIMES.times { s.delete(" ") }
end
x.report("tr") do
TIMES.times { s.tr(" ", "") }
end
x.report("gsub with regexp") do
TIMES.times { s.gsub(/ /, "") }
end
x.report("gsub with string") do
TIMES.times { s.gsub(" ", "") }
end
end
# Rehearsal ----------------------------------------------------
# delete 3.640000 0.010000 3.650000 ( 3.726573)
# tr 3.730000 0.000000 3.730000 ( 3.728782)
# gsub with regexp 6.570000 0.010000 6.580000 ( 6.573503)
# gsub with string 7.980000 0.010000 7.990000 ( 7.993387)
# ------------------------------------------ total: 21.950000sec
# user system total real
# delete 3.450000 0.010000 3.460000 ( 3.444691)
# tr 3.920000 0.000000 3.920000 ( 3.919492)
# gsub with regexp 6.420000 0.010000 6.430000 ( 6.437928)
# gsub with string 8.290000 0.010000 8.300000 ( 8.322401)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment