From: Robert Dober Date: 2006-03-29T21:33:33+09:00 Subject: Re: strings and ruby style? ------=_Part_379_17688623.1143635597208 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On 3/29/06, Dave Burt wrote: There was a different error I forgot to initialize "actual" Dave Excellent idea "<<". I do not see where there are less objects in << than in +=3D actually. However << is *the* operator to use. I came up with a benchmark constructin= g much longer strings your << (or concat) is the *best* solution. Another approach is not to construct strings immedeatly but to build an array and to join the array eventually. For long strings the result is quit= e speactacular (almost as fast as << ) Pleas kindly look at this -------------------------------9<------------------------------------------ 647/147 > cat string2.rb;ruby string2.rb #!/usr/bin/env ruby require 'benchmark' List =3D %w{ omega delta Ringo } * 50 n =3D 5000 Benchmark.bm do |bm| bm.report "explicit +" do n.times do actual =3D '' List.each do |x| actual =3D actual + x end end end # do bm.report "implicit +" do n.times do actual =3D '' List.each do |x| actual +=3D x end end end # do bm.report "append " do n.times do actual =3D '' List.each do |x| actual << x end end end bm.report "Array " do n.times do actual =3D [] List.each do |x| actual << x end actual =3D actual.join("") end end end user system total real explicit + 2.170000 0.010000 2.180000 ( 2.336149) implicit + 2.170000 0.010000 2.180000 ( 2.635293) append 0.730000 0.000000 0.730000 ( 0.800968) Array 0.770000 0.000000 0.770000 ( 0.862098) --------------------------------------10<------------------------------- Cheers Robert -- Deux choses sont infinies : l'univers et la b=EAtise humaine ; en ce qui concerne l'univers, je n'en ai pas acquis la certitude absolue. - Albert Einstein ------=_Part_379_17688623.1143635597208--