Skip to content

Instantly share code, notes, and snippets.

@jish
Created October 3, 2010 19:22
Show Gist options
  • Save jish/608836 to your computer and use it in GitHub Desktop.
Save jish/608836 to your computer and use it in GitHub Desktop.
require 'benchmark'
n = 1_000_000
Benchmark.bm(7) do |x|
x.report('gsub') { for i in 1..n; 'hello'.gsub('l', ''); end }
x.report('tr') { for i in 1..n; 'hello'.tr('l', ''); end }
x.report('delete') { for i in 1..n; 'hello'.delete('l'); end }
end
# user system total real
# gsub 4.230000 0.000000 4.230000 ( 4.230689)
# tr 1.230000 0.000000 1.230000 ( 1.231086)
# delete 1.090000 0.010000 1.100000 ( 1.092491)
# user system total real
# gsub 4.200000 0.000000 4.200000 ( 4.202974)
# tr 1.220000 0.010000 1.230000 ( 1.220776)
# delete 1.100000 0.000000 1.100000 ( 1.097038)
# user system total real
# gsub 4.170000 0.000000 4.170000 ( 4.178987)
# tr 1.230000 0.000000 1.230000 ( 1.225010)
# delete 1.090000 0.000000 1.090000 ( 1.089195)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment