[SVN] r3879 (TextMate)

James Edward Gray II james at grayproductions.net
Thu Jun 29 13:50:52 UTC 2006


On Jun 28, 2006, at 5:01 AM, Allan Odgaard wrote:

> • use ‘<<’ instead of ‘+=’ when appending to strings, as the former  
> is magnitudes faster

Oops, I misread this line yesterday.  I thought you were replacing <<  
with +=, which seemed very odd to me, because I was almost sure << is  
faster.  ;)

Now that we are on the same page here, the reason is:

+= makes a new String with each call, then appends the content to  
that.  So it's at least twice as much work, not counting that it  
needs to reassign to the variable.  All those extra Strings can  
trigger GC fairly easy too, which is always going to kill timing  
results.  Finally, Ruby literally translates `str += "a"` to `str =  
str + "a"` at parse time and much of the work in solving an  
expression like that must be done up it the Ruby layer, while `str <<  
"a"` happens almost completely in the C layer.

Hope that explains it a little.

James Edward Gray II




More information about the textmate-dev mailing list