On Tue, Mar 18, 2008 at 02:40:43PM +0900, Gerardo Santana G?mez Garrido wrote:
> $ 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

I think you are demonstrating the difference between IO#puts and
IO#write (which still surprised me).

cout@bean:~/tmp$ time ruby a.rb 1_000_000 > /dev/null

real    0m2.769s
user    0m2.660s
sys     0m0.044s
cout@bean:~/tmp$ time ruby b.rb 1_000_000 > /dev/null

real    0m1.315s
user    0m1.244s
sys     0m0.020s
cout@bean:~/tmp$ time ruby c.rb 1_000_000 > /dev/null

real    0m1.256s
user    0m1.160s
sys     0m0.036s
cout@bean:~/tmp$ cat c.rb
times = ARGV[0].to_i
times.times { $stdout.write "hola mundo\n" }

And on my machine with 10_000_000 iterations I starting digging into swap with
the stringio solution:

cout@bean:~/tmp$ time ruby a.rb 10_000_000 > /dev/null

real    0m27.841s
user    0m25.694s
sys     0m0.112s
cout@bean:~/tmp$ time ruby b.rb 10_000_000 > /dev/null

real    0m54.888s
user    0m11.333s
sys     0m0.504s
cout@bean:~/tmp$ time ruby c.rb 10_000_000 > /dev/null

real    0m13.141s
user    0m11.749s
sys     0m0.100s

Paul