Skip to content

Instantly share code, notes, and snippets.

@dominikh
Created October 13, 2009 02:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dominikh/208915 to your computer and use it in GitHub Desktop.
Save dominikh/208915 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("rin") }
end
x.report("multiple delete") do
TIMES.times { s.delete("r").delete("i").delete("n") }
end
x.report("tr") do
TIMES.times { s.tr("rin", "") }
end
x.report("gsub with regexp") do
TIMES.times { s.gsub(/[rin]/, "") }
end
end
# Rehearsal ----------------------------------------------------
# delete 2.030000 0.010000 2.040000 ( 2.028561)
# multiple delete 5.710000 0.010000 5.720000 ( 5.718976)
# tr 2.380000 0.000000 2.380000 ( 2.376503)
# gsub with regexp 5.780000 0.020000 5.800000 ( 5.810256)
# ------------------------------------------ total: 15.940000sec
# user system total real
# delete 2.030000 0.000000 2.030000 ( 2.026632)
# multiple delete 5.730000 0.010000 5.740000 ( 5.739280)
# tr 2.390000 0.000000 2.390000 ( 2.388175)
# gsub with regexp 5.820000 0.010000 5.830000 ( 5.836098)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment