Please note, new blog at http://www.acheron.org/darryl/

Using Arrays for String Concatenation

In response to my previous post about using Java for String concatenation instead of ColdFusion’s concatenation operator, Greg has given me an interesting tip. It turns out that ArrayAppend() is a LOT quicker than regular concatenation, and the StringBuffer class! If you’re generating a CSV string, all you have to do is put each line into an array element using the ArrayAppend() method. Once all lines have been appended, convert the array to a list using the ArrayToList() method, using #chr(13)##chr(10)# as the delimiter. I decided to run the same sorts of tests that I have before. This time, I compared concatenating a string with regular string concatenation, the ArrayAppend method and the Java StringBuffer class. Using ArrayAppend was certainly the quicker of the three – by a long way. So, if you’re building a small-medium length string, then ArrayAppend is the way to go. However, a word of caution. If you are creating a large CSV file, then use the Java methods I have discussed before.

Results

Code Examples

Regular string concatenation <cftimer type="inline" label="Regular concentation"> <cfscript> iNrTimesToLoop = url.nr; sStringToConcat = "The quick brown fox jumped over the fence: "; sString = ""; for (x=1; x LTE iNrTimesToLoop; x=x+1) { sString = sString & sStringToConcat & x; } </cfscript> </cftimer> ArrayAppend <cftimer type="inline" label="ArrayAppend"> <cfscript> iNrTimesToLoop = url.nr; sStringToConcat = "The quick brown fox jumped over the fence: "; aString = ArrayNew(1); for (x=1; x LTE iNrTimesToLoop; x=x+1) { temp = ArrayAppend(aString, sStringToConcat & x); } // Convert back into a string sString = ArrayToList(aString, "#chr(13)##chr(10)#"); </cfscript> </cftimer>

By Anonymous mike lyda, at 10/03/2005 03:24:00 am  

I put together a similar test for Flash.. diferent results entirely of course..

http://oddhammer.com/actionscriptperformance/set3/index.html
(test #15)



By Anonymous Rahul Narula, at 10/06/2005 03:57:00 am  

<cftimer type="inline" label="ListAppend">
<cfscript>
iNrTimesToLoop = url.nr;
sStringToConcat = "The quick brown fox jumped over the fence: ";
aString = "";

for (x=1; x LTE iNrTimesToLoop; x=x+1)
{
temp = ListAppend(aString, sStringToConcat & x);
}

</cfscript>
</cftimer>


We achieve better times as we avoided using 2 function calls
- ArrayNew(1)
- ArraytoList()



By Anonymous Rahul Narula, at 10/06/2005 04:03:00 am  

try url.nr > 20000 to see some appreciable diff between the 2 methods



By Blogger J&D, at 9/18/2009 10:42:00 pm  

av女優
av情人趣味用品愛爾蘭情人趣味愛蜜莉情人趣味用品情趣用品

情趣用品情趣用品aio交友愛情館一葉情貼圖片區一葉晴貼影片區



By Blogger longge, at 6/26/2010 01:15:00 pm  

While traveling anywhere and the Louis vuitton bags
is the one which makes you beautiful wherever you go. This is not alone due to the architecture of the atypical louis vuitton handbags
. lv
accept got their own accent in our lives, for arcade or anywhere abroad after accustomed a louis vuitton
with us.



By Blogger dingdang, at 6/28/2010 04:58:00 pm  

One of the ultimate appearance accessories in the apple is the hermes handbag
. Why is the Kelly bag
such a account of elitism?One of the affidavit is due to the superior of the Kelly bags
.
Currently supports and how to use open-source tools to construct a GOMER PYLE
that can play on almost every GOMER PYLE DVD
player. A GOMER PYLE DVD COLLECTION
is comprised of one or more video title sets.



» Post a Comment

Trackbacks:

» Create a Link