From: Alexandre Hausen Date: 2009-06-20T03:15:07+09:00 Subject: Re: String += vs << Classical Copy&Paste bug ;-) Lines 8 and 14 both read "a += b" in strings.rb. However line 14 should read "a << b". -- Alexandre On Jun 18, 5:08 am, Robert Dober wrote: > On Wed, Jun 17, 2009 at 7:29 PM, pat eyler wrote: > > that's a nice article about some real-world benchmarking.  I wish > > more people did things like this. > > If you search the archives you might find a certain Robert preaching, > never to use a += b when sequences were concerned. Do I feel clever > now? No rather stupid. > > Appologies for the lengthy code snippets. > > Although I fully acknowledge the value of the post and that it might > be a life saver I would like to add that I pretty much have the > feeling that immutable is preferable over mutable. > And it seems that modern VMs (jruby, 1.9, ???)  kind of are written > for that programming style. I am also aware that they make micro > benchmarks like the following even less meaningless, but please > consider it just as a Whack On The Head (nonviolently of course). > > --------------------------------------------------------- > 512/19 > cat strings.rb > > N = 10_000 > b = "Wassitmean" > require 'benchmark' > Benchmark.bmbm do | bench | >   a = "Ruby Rules Re Rowld" >   bench.report "+=" do >     N.times do >       a += b >     end >   end >   a = "Ruby Rules Re Rowld" >   bench.report "<<" do >     N.times do >       a += b >     end >   end > end > > 513/20 > jruby -v strings.rb > jruby 1.3.0 (ruby 1.8.6p287) (2009-06-06 6586) (OpenJDK Client VM > 1.6.0_0) [i386-java] > Rehearsal -------------------------------------- > +=   1.256000   0.000000   1.256000 (  1.191000) > <<   9.384000   0.000000   9.384000 (  9.384000) > ---------------------------- total: 10.640000sec > >          user     system      total        real > +=  23.397000   0.000000  23.397000 ( 23.397000) > <<  52.953000   0.000000  52.953000 ( 52.953000) > > ruby 1.9.1p129 (2009-05-12 revision 23412) [i686-linux] > Rehearsal -------------------------------------- > +=   0.360000   0.020000   0.380000 (  0.406038) > <<   1.040000   0.130000   1.170000 (  1.209839) > ----------------------------- total: 1.550000sec > >          user     system      total        real > +=   1.770000   0.230000   2.000000 (  2.056577) > <<   2.410000   0.240000   2.650000 (  3.456429) > > I believe that I hit the GC in JRuby with the default settings and the > above might be an indication how performing > the short time object allocation is nowadays. Ruby1.9 has enough > memory on my machine to be that fast but still += is faster than <<. > > Cheers > Robert > > -- > Toutes les grandes personnes ont d’abord été des enfants, mais peu > d’entre elles s’en souviennent. > > All adults have been children first, but not many remember. > > [Antoine de Saint-Exupéry]