On 6/2/05, Nikolai Weibull <mailing-lists.ruby-talk / rawuncut.elitemail.org> wrote: > > And some aren't. Anyway, I've had gsub! be a lot slower than gsub in > benchmarks. Still, some destructive methods will by their very > definition be faster. > > I'd say that a time-difference of 0.000235 seconds per call to > String#sort!.reverse! versus String#sort.reverse doesn't warrant > destructive methods on strings (in this benchmark anyway), > OK, you're right in case of String#gsub and String#gsub!. But not 'a lot slower' but 'a little slower'. $ cat destructive.rb require 'benchmark' Benchmark.bmbm do |bm| bm.report("Destructive") do 100.times do str = (0..10000).to_a.join('-') str.gsub!(/\d+/, '-') end end bm.report("Non-destructive") do 100.times do str = (0..10000).to_a.join('-') str = str.gsub(/\d+/, '-') end end end $ ruby destructive.rb Rehearsal --------------------------------------------------- Destructive 4.240000 0.010000 4.250000 ( 4.448997) Non-destructive 4.250000 0.010000 4.260000 ( 4.279959) ------------------------------------------ total: 8.510000sec user system total real Destructive 4.260000 0.010000 4.270000 ( 4.378810) Non-destructive 4.230000 0.010000 4.240000 ( 4.305321) I don't understand what exactly this difference implies. Does that mean Ruby's String is much alike raw C string? Or is there any particular optimization for String copy? Anyway, I don't like early optimization too in your sense. But in most case except String, bang method will be much faster than normal methods which return new value. This difference is somewhat meaningful, at least, than your case. I have a strong affection on method chaining, so mostly I don't use bang method. But I should admit that bang version of same method would be somewhat faster. -- http://nohmad.sub-port.net