On Wed, 10 Jan 2007, Gavin Kistner wrote:
> From: Vincent Fourmond
>> user system total real
>> 0.010000 0.000000 0.010000 ( 0.009087)
>> 0.010000 0.000000 0.010000 ( 0.008774)
>> 0.000000 0.000000 0.000000 ( 0.004621)
>
> Perhaps your machine is more deterministic than mine, but successive
> runs of that benchmark (and using #bmbm to be safer about the
> measurement) sometimes show 'a' faster than "a", sometimes slower.
>
> Even with benchmarking, I wouldn't trust that answers that are within a
> few percent of each other. And I certainly wouldn't rush off to refactor
> code because of it.
Increase n from 5000 to 500000 or 5000000.
To understand the difference, just think about how many strings are being
created with each.
'a' creates a new string, as does 'b'.
The + operation creates a new string, as well.
So, there's a lot of new string creation happening with either of the +
examples.
Change the +'s to << and you will see a difference.
'a' << x << 'b'
<< just changes the old String.
The "a#{x}b" example does the least work.
Kirk Haines