From: James Tucker Date: 2008-03-19T20:17:23+09:00 Subject: Re: Ruby "Speedup" hints? On 18 Mar 2008, at 05:40, Gerardo Santana Gómez Garrido wrote: > On Mon, Mar 17, 2008 at 6:08 PM, Paul Brannan > wrote: >> >> On Tue, Mar 18, 2008 at 02:17:22AM +0900, Gerardo Santana G?mez >> Garrido wrote: >>> On Mon, Mar 17, 2008 at 12:42 PM, Marc Heiler >> > wrote: >>>> Anyone of you has a few hints on how to speed up ruby code? >>>> (Note - not writing it, but running it ;-) ) >>>> >>>> I only have a few hints, like 5... would love to extend it. >>> >>> When generating text output, using StringIO is faster than using >>> puts >> >> This doesn't make sense. One can easily call #puts on a StringIO >> object. >> >> Do you have a concrete example/benchmark that demonstrates the >> difference? >> >> Paul >> >> >> > > $ time ruby a.rb 10_000_000 > /tmp/a && sleep 3 && time ruby b.rb > 10_000_000 > /tmp/b; printf "\a" > > real 1m45.305s > user 1m27.581s > sys 0m17.715s > > real 0m59.049s > user 0m41.984s > sys 0m16.997s > $ cat a.rb > times = ARGV[0].to_i > times.times { puts "hola mundo" } > $ cat b.rb > require 'stringio' > > times = ARGV[0].to_i > output = StringIO.new > times.times { output.write("hola mundo\n") } > output.rewind > print output.read > > > The difference increases in real world reports. > -- > Gerardo Santana > Do you not realize that this is fundamentally telling you your terminal is super-slow? This has nothing to do with ruby itself, really. Try it on some other platforms and tool stacks, for goodness sake. And FYI, this is why context specific profiling is far more important than any of these strange idioms you're talking about. Oh, and this is only really true for short strings and relatively small amounts of memory. Try doing that whilst say, stream editing a 1TB file. This whole direction of thought is broken, honestly.