From: Gyoung-Yoon Noh Date: 2005-06-02T04:09:03+09:00 Subject: Re: Method Chaining Issues On 6/2/05, Nikolai Weibull wrote: > It should also be pointed out that the non-destructive version of sub > (i.e., String#sub) method often turns out to be faster than the > destructive one (i.e., String#sub!), > nikolai > No, what you said are inverted. Some non-destructive methods are usually implemented by simply copying reference after calling destructive method. It looks like: def sort(ary): sort!(ary) return ary end This is a benchmark results. $ cat destructive.rb require 'benchmark' Benchmark.bmbm do |bm| bm.report("Destructive") do 1000.times do ary = (0..10000).to_a ary.reverse! ary.sort! end end bm.report("Non-destructive") do 1000.times do ary = (0..10000).to_a ary.reverse.sort end end end $ ruby destructive.rb Rehearsal --------------------------------------------------- Destructive 2.910000 0.000000 2.910000 ( 2.904260) Non-destructive 3.110000 0.000000 3.110000 ( 3.116915) ------------------------------------------ total: 6.020000sec user system total real Destructive 2.930000 0.000000 2.930000 ( 2.935663) Non-destructive 3.170000 0.010000 3.180000 ( 3.171050) -- http://nohmad.sub-port.net