Skip to content

Instantly share code, notes, and snippets.

@xaviershay
Created November 18, 2008 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xaviershay/26076 to your computer and use it in GitHub Desktop.
Save xaviershay/26076 to your computer and use it in GitHub Desktop.
require 'benchmark'
n = 100000
Benchmark.bmbm do |x|
x.report('tr') { n.times { "created-at".tr( '-', '_') }}
x.report('gsub') { n.times { "created-at".gsub('-', '_') }}
end
<<-EOS
~/Code/sandbox/misc (master)$ ruby tr_gsub_benchmark.rb
Rehearsal ----------------------------------------
tr 0.120000 0.000000 0.120000 ( 0.125310)
gsub 0.210000 0.000000 0.210000 ( 0.222762)
------------------------------- total: 0.330000sec
user system total real
tr 0.110000 0.000000 0.110000 ( 0.112288)
gsub 0.220000 0.000000 0.220000 ( 0.230003)
~/Code/sandbox/misc (master)$ ruby -v
ruby 1.8.6 (2007-09-23 patchlevel 110) [i686-darwin8.11.1]
EOS
Benchmark.bmbm do |x|
x.report('tr') { n.times { "created-at".tr( '-', '') }}
x.report('gsub') { n.times { "created-at".gsub('-', '') }}
x.report('delete') { n.times { "created-at".delete('-') }}
end
<<-EOS
Rehearsal ------------------------------------------
tr 0.210000 0.000000 0.210000 ( 0.229333)
gsub 0.190000 0.000000 0.190000 ( 0.194046)
delete 0.200000 0.000000 0.200000 ( 0.211297)
--------------------------------- total: 0.600000sec
user system total real
tr 0.210000 0.000000 0.210000 ( 0.224744)
gsub 0.190000 0.010000 0.200000 ( 0.196630)
delete 0.200000 0.000000 0.200000 ( 0.205748)
EOS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment