On Mon, Mar 17, 2008 at 6:08 PM, Paul Brannan <pbrannan / atdesk.com> 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 <shevegen / linuxmail.org> 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