Nikolai Weibull wrote:
> * Daniel Amelang (Mar 19, 2005 23:30):
> > Perhaps a more interesting solution would be to get rid of all
> > destructive methods and work on optimizing ther existing
> > non-destructive counterparts. Matz seems to prefer this solution.
>
> > http://ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/17764
>
> > Unfortunately, the non-destructive versions are still quite
noticeably
> > slower.
>
> No they're not.  Where are you getting these assertions from?,
>         nikolai

Oh, I dunno.  Maybe this benchmark?

The only exception was slice vs. slice!.  No idea what happened there.

Here are the results of my benchmark running on a 1.8 GHz P4, 512MB
RAM, on Windows XP, SP2.  The actual benchmark code I used it further
below.

C:\eclipse\workspace\ruby-foo\benchmarks>ruby bang.rb
      user     system      total        real
capitalize  1.862000   0.000000   1.862000 (  1.923000)
capitalize!  0.591000   0.000000   0.591000 (  0.600000)
chomp  1.663000   0.010000   1.673000 (  1.853000)
chomp!  0.510000   0.000000   0.510000 (  0.511000)
delete  5.739000   0.030000   5.769000 (  6.449000)
delete!  4.396000   0.000000   4.396000 (  4.606000)
downcase  1.712000   0.010000   1.722000 (  2.294000)
downcase!  0.581000   0.000000   0.581000 (  0.580000)
gsub  4.296000   0.030000   4.326000 (  5.188000)
gsub!  0.972000   0.000000   0.972000 (  1.161000)
lstrip  1.762000   0.020000   1.782000 (  2.123000)
lstrip!  0.431000   0.000000   0.431000 (  0.431000)
next  1.813000   0.000000   1.813000 (  1.933000)
next!  1.331000   0.010000   1.341000 (  1.472000)
reverse  1.633000   0.010000   1.643000 (  1.742000)
reverse!  0.380000   0.000000   0.380000 (  0.501000)
rstrip  1.773000   0.000000   1.773000 (  1.882000)
rstrip!  0.420000   0.000000   0.420000 (  0.431000)
slice  1.883000   0.020000   1.903000 (  2.454000)
slice!  3.415000   0.010000   3.425000 (  3.765000)
strip  1.793000   0.000000   1.793000 (  1.812000)
strip!  0.370000   0.000000   0.370000 (  0.411000)
sub  1.302000   0.000000   1.302000 (  1.382000)
sub!  0.901000   0.000000   0.901000 (  1.202000)
swapcase  0.862000   0.000000   0.862000 (  0.961000)
swapcase!  0.420000   0.000000   0.420000 (  0.451000)
tr  3.515000   0.000000   3.515000 (  3.925000)
tr  2.284000   0.000000   2.284000 (  2.484000)
tr_s  3.545000   0.010000   3.555000 (  3.845000)
tr_s!  2.193000   0.000000   2.193000 (  2.303000)
upcase  2.023000   0.000000   2.023000 (  2.103000)
upcase!  0.631000   0.000000   0.631000 (  0.651000)

require "benchmark"
include Benchmark

s1 = " hello "
s2 = " hello "
s3 = " hello "
max = 500000

bm do |bench|
   bench.report("capitalize"){
      max.times{ s1.capitalize }
   }

   bench.report("capitalize!"){
      max.times{ s1.capitalize! }
   }

   bench.report("chomp"){
      max.times{ s1.chomp }
   }

   bench.report("chomp!"){
      max.times{ s1.chomp! }
   }

   bench.report("delete"){
      max.times{ s1.delete("o") }
   }

   bench.report("delete!"){
      max.times{ s1.delete!("o") }
   }

   bench.report("downcase"){
      max.times{ s1.downcase }
   }

   bench.report("downcase!"){
      max.times{ s1.downcase! }
   }

   bench.report("gsub"){
      max.times{ s1.gsub(/e/,"i") }
   }

   bench.report("gsub!"){
      max.times{ s1.gsub!(/e/,"i") }
   }

   bench.report("lstrip"){
      max.times{ s1.lstrip }
   }

   bench.report("lstrip!"){
      max.times{ s1.lstrip! }
   }

   bench.report("next"){
      max.times{ s1.next }
   }

   bench.report("next!"){
      max.times{ s1.next! }
   }

   bench.report("reverse"){
      max.times{ s1.reverse }
   }

   bench.report("reverse!"){
      max.times{ s1.reverse! }
   }

   bench.report("rstrip"){
      max.times{ s1.rstrip }
   }

   bench.report("rstrip!"){
      max.times{ s1.rstrip! }
   }

   bench.report("slice"){
      max.times{ s2.slice(1..4) }
   }

   bench.report("slice!"){
      max.times{ s2.slice!(1..4) }
   }

   bench.report("strip"){
      max.times{ s2.strip }
   }

   bench.report("strip!"){
      max.times{ s2.strip! }
   }

   bench.report("sub"){
      max.times{ s2.sub(/e/,"i") }
   }

   bench.report("sub!"){
      max.times{ s2.sub!(/e/,"i") }
   }

   bench.report("swapcase"){
      max.times{ s2.swapcase }
   }

   bench.report("swapcase!"){
      max.times{ s2.swapcase! }
   }

   bench.report("tr"){
      max.times{ s3.tr("e","a") }
   }

   bench.report("tr"){
      max.times{ s3.tr!("e","a") }
   }

   bench.report("tr_s"){
      max.times{ s3.tr_s("a","e") }
   }

   bench.report("tr_s!"){
      max.times{ s3.tr_s!("a","e") }
   }

   bench.report("upcase"){
      max.times{ s3.upcase }
   }

   bench.report("upcase!"){
      max.times{ s3.upcase! }
   }
end

Regards,

Dan