On 18 Mar 2008, at 05:40, Gerardo Santana G=F3mez Garrido wrote:

> On Mon, Mar 17, 2008 at 6:08 PM, Paul Brannan <pbrannan / atdesk.com> =20=

> wrote:
>>
>> On Tue, Mar 18, 2008 at 02:17:22AM +0900, Gerardo Santana G?mez =20
>> Garrido wrote:
>>> On Mon, Mar 17, 2008 at 12:42 PM, Marc Heiler =
<shevegen / linuxmail.org=20
>>> > 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 =20
>>> 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 =3D ARGV[0].to_i
> times.times { puts "hola mundo" }
> $ cat b.rb
> require 'stringio'
>
> times =3D ARGV[0].to_i
> output =3D StringIO.new
> times.times { output.write("hola mundo\n") }
> output.rewind
> print output.read
>
>
> The difference increases in real world reports.
> --=20
> Gerardo Santana
>


Do you not realize that this is fundamentally telling you your =20
terminal is super-slow? This has nothing to do with ruby itself, =20
really. Try it on some other platforms and tool stacks, for goodness =20
sake.

And FYI, this is why context specific profiling is far more important =20=

than any of these strange idioms you're talking about.

Oh, and this is only really true for short strings and relatively =20
small amounts of memory. Try doing that whilst say, stream editing a =20
1TB file.

This whole direction of thought is broken, honestly.=