"Sean O'Halpin" <sean.ohalpin / gmail.com> writes:

> On 7/27/06, Mat Schaffer <schapht / gmail.com> wrote:
>> I just did a quick benchmark to prove something to myself.  But I'd
>> like to get a sanity check from the people on the list.
>>
> Using Ara's code:
>
> Rehearsal --------------------------------------------
> Indexing   0.109000   0.000000   0.109000 (  0.109000)
> Chop       6.766000   8.250000  15.016000 ( 15.110000)
> Chop2      2.656000   3.781000   6.437000 (  6.468000)
> ---------------------------------- total: 21.562000sec
>
>                user     system      total        real
> Indexing   0.156000   0.000000   0.156000 (  0.156000)
> Chop       0.094000   0.000000   0.094000 (  0.094000)
> Chop2      0.187000   0.000000   0.187000 (  0.187000)
>
>> ruby -v
> ruby 1.8.4 (2005-12-24) [i386-mswin32]
>
> I think the difference in performance is because internally chop does
> a dup on the string then calls chop! whereas the index operation
> creates a new string which shares the old string but with a different
> length. I guess this is also why the rehearsal and final results
> differ - cutting out the cost of GC doesn't reflect the true cost of
> using chop (especially with big strings).
>
> Regards,
> Sean

Indexing allocates a new string.  It has to since (1) Ruby strings are
mutable, (2) Ruby strings have \0 at the end.

Steve