From: Robert Klemme Date: 2008-03-12T06:45:27+09:00 Subject: Re: duplicating characters in a string On 11.03.2008 19:53, William James wrote: Btw, is there any particular reason why you duplicated the mail content? > On Mar 11, 9:26 am, Robert Klemme wrote: >> 2008/3/11, William James : >> >>> On Mar 7, 11:45 pm, Joel VanderWerf wrote: >>> > Rehearsal --------------------------------------------------- >>> > s.gsub(re){...} 6.220000 0.010000 6.230000 ( 6.325230) >>> > s.gsub(re,...) 3.020000 0.060000 3.080000 ( 3.115286) >>> > 7stud 4.110000 0.000000 4.110000 ( 4.142912) >>> > 7stud 2.050000 0.000000 2.050000 ( 2.072398) >>> > ----------------------------------------- total: 15.470000sec >>> > user system total real >>> > s.gsub(re){...} 5.910000 0.020000 5.930000 ( 5.993456) >>> > s.gsub(re,...) 3.000000 0.000000 3.000000 ( 3.017119) >>> > 7stud 4.100000 0.020000 4.120000 ( 4.300772) >>> > 7stud 2.030000 0.010000 2.040000 ( 2.068190) >>> Pascal (FreePascal): >>> uses sysutils; // for timing >>> function dup_chars( s: ansistring ): ansistring; >>> var >>> out: ansistring; >>> i,j: longint; >>> c: char; >>> begin >>> setlength( out, length(s) * 2 ); >>> for i := 1 to length(s) do >>> begin >>> c := s[i]; j := (i shl 1); >>> out[j-1] := c; >>> out[j] := c; >>> end; >>> exit( out ) >>> end; >>> var >>> s : ansistring; >>> i : longint; >>> when : tDateTime; >>> begin >>> s := ''; >>> for i:=1 to 1000000 do s += 'abc'; >>> when := Time; >>> dup_chars( s ); >>> writeln( ((time - when) * secsPerDay):0:3, ' seconds' ) >>> end. >>> --->0.031 seconds >> Your point being? >> >> robert >> >> -- >> use.inject do |as, often| as.you_can - without end > > On my computer: > > s.gsub(re, ...) --> 9.641000 seconds > Lua --> 1.078 seconds > > Ruby's gsub is far too slow. 7stud even beat it with an each_byte > loop! > Disgraceful. Disgusting. Inexcusable. > > The Lua code: > > s = string.rep( 'abc', 1000000 ) > > time = os.clock() > string.gsub( s, '(.)', "%1%1" ) > print( os.clock() - time, "seconds" ) I do not know Lua. Does this return a copied string or does it do it inplace? Kind regards robert