String Substitution

Jul 30
2008
require 'benchmark'
 
n = 1_000_000
s = " : : : "
 
Benchmark.bm do |x|
  x.report("tr:")   { n.times{ s.tr(":", ";") } }
  x.report("tr_s:") { n.times{ s.tr_s(":", ";") } }
  x.report("gsub:") { n.times{ s.gsub(":", ";") } }
end

Right, tr beats the other methods:
user system total real
tr: 2.760000 0.120000 2.880000 ( 2.897022)
tr_s: 5.310000 0.240000 5.550000 ( 5.822026)
gsub: 8.850000 0.200000 9.050000 ( 9.311452)

Calendar

February 2012
M T W T F S S
« Sep    
 12345
6789101112
13141516171819
20212223242526
272829  

Tags