2008/3/19, Paul Brannan <pbrannan / atdesk.com>:
> On Thu, Mar 20, 2008 at 01:44:11AM +0900, Gerardo Santana G?mez Garrido wrote:
>  > Replace output.write("hola mundo\n") with output.puts("hola mundo")
>  > and you'll get something similar.
>
>
> Not much difference, and the StringIO solution uses more memory.  Not
>  surprising, since both File#puts and StringIO#puts are implemented by
>  rb_io_puts.
>
>  The difference may be more dramatic on your platform, depending on the
>  implementation of your C library's stdio functions.

I'd say the difference is not worthwhile the added complexity and
memory overhead of StringIO:

18:35:19 /cygdrive/c/SCMws/
$ ruby <<XX
> require 'stringio'
> require 'benchmark'
>
> File.open('/dev/null', 'wb') do |devnull|
>   Benchmark.bmbm(15) do |x|
>    n = 2_000_000
>
>    x.report("IO#puts") {
>      n.times do
>        devnull.puts "hola"
>      end
>    }
>
>    x.report("IO#write") {
>      n.times do
>        devnull.write "hola\n"
>      end
>    }
>
>    x.report("StringIO#puts") {
>      s = StringIO.new
>      n.times do
>        s.puts "hola"
>      end
>      s.rewind
>      devnull.print s.read
>    }
>
>    x.report("StringIO#write") {
>      s = StringIO.new
>      n.times do
>        s.write "hola\n"
>      end
>      s.rewind
>      devnull.print s.read
>    }
>   end
> end
> XX
Rehearsal --------------------------------------------------
IO#puts          5.984000   0.016000   6.000000 (  6.000000)
IO#write         2.875000   0.000000   2.875000 (  2.885000)
StringIO#puts    4.219000   0.016000   4.235000 (  4.240000)
StringIO#write   2.078000   0.031000   2.109000 (  2.113000)
---------------------------------------- total: 15.219000sec

                     user     system      total        real
IO#puts          6.000000   0.000000   6.000000 (  5.996000)
IO#write         2.875000   0.000000   2.875000 (  2.871000)
StringIO#puts    4.203000   0.015000   4.218000 (  4.224000)
StringIO#write   2.140000   0.047000   2.187000 (  2.175000)
18:36:46 /cygdrive/c/SCMws/
$

Note: I added proper closing of devnull and made the stream binary.

Kind regards

robert