String Substitution

Posted by root Wed, 30 Jul 2008 13:59:00 GMT

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)

Posted in  | Tags  | no comments

Comments

Comments are disabled