My Projects
Search Blog

Categories
Archives
Useful Links
Photo Albums
RSS

Powered by
BlogCFM v1.15

Vivio Technologies VPS Hosting
18 August 2006
CFML String Concatenation is SLOW! Use Java StringBuffer instead

I was trying to speed up a process of mine that involves a large amount of string concatenation.

In other words, <cfset str = str & someOtherString>

Turns out there's a much faster way.  In my case, I was dynamically generating an data file from a query.  Each row had let's say 50 fields.  Generation of each row required 3 string concatenations at a time (maybe more, depending on how it worked when compiled to the servlet).  Do a drop of 5000 records, and well, that's a lot of string concats!  750,000 at a minimum.

Someone on cf-talk suggested using a StringBuffer might be wise because Coldfusion probably creates a new string object every time you do <cfset foo = foo & foo2>.  So I decided to write some code to test it out.

Sure enough, using the java StringBuffer class is quite a lot faster.  On my local machine, I ran the following code (see below).  The CFML method completed in 89 seconds, while the java StringBuffer method completed in only 28 seconds - about 1/3 of the time!

Here's the code, if you're interested:

<cfset a = "">
<cfset starttime = now()>
<cfset cnt = 0>
<cfloop index="i" from="1" to="2000000">
    <cfset a = a & "This is some crazy stuff. ">
    <cfset cnt = cnt + 26>
    <cfif i mod 1000 is 0>
        <!--- write to file and reset string to empty --->
        <cfset a = "">
    </cfif>
</cfloop>
<cfoutput><p>Done with #cnt# bytes in #abs(dateDiff('s',Now(),starttime))# seconds.</p></cfoutput>

<cfset a = createObject("java","java.lang.StringBuffer")>
<cfset starttime = now()>
<cfset cnt = 0>
<cfloop index="i" from="1" to="2000000">
    <cfset a.append("This is some crazy stuff. ")>
    <cfset cnt = cnt + 26>
    <cfif i mod 10000 is 0>
        <!--- write to file and reset string to empty --->
        <cfset a = createObject("java","java.lang.StringBuffer")>
    </cfif>
</cfloop>
<cfoutput><p>Done with #cnt# bytes in #abs(dateDiff('s',Now(),starttime))# seconds.</p></cfoutput>

 

 

Posted by rickroot at 7:38 AM | Link | 5 comments
Subscription Options

You are not logged in, so your subscription status for this entry is unknown. You can login or register here.

Re: CFML String Concatenation is SLOW! Use Java StringBuffer instead
I wrote a UDF which converts a Query to CSV using the Java StringBuffer. Here is the link

http://www.cflib.org/udf.cfm?ID=1197

HTH
Posted by kaasu on August 18, 2006 at 9:31 AM

Re: CFML String Concatenation is SLOW! Use Java StringBuffer instead
Your code wouldn't work for strings that contained quotes.

This line:

csv.append( '"' & query[cols[j]][i] & '",' );

probably ought to double up the double quotes inside. Example

"This is a ""real"" csv-formatted string."

I've got an app where I use some code similar to your UDF, but mine will generate CSV, tab-delimited, or HTML tables (aka, EXCEL). Mine's not a UDF though cuz I'm appending to a file every X lines, to avoid having the gigantic variable in memory - some of our file drops can be quite large!

Rick
Posted by rickroot on August 18, 2006 at 10:58 AM

Re: CFML String Concatenation is SLOW! Use Java StringBuffer instead
Rick - would you mind sharing your code to "will generate CSV, tab-delimited, or HTML tables (aka, EXCEL)."?

thanks!
Posted by aqlong on December 18, 2006 at 8:35 AM

Re: CFML String Concatenation is SLOW! Use Java StringBuffer instead
Posted by rickroot on December 18, 2006 at 9:43 AM

Re: CFML String Concatenation is SLOW! Use Java StringBuffer instead
Thanks Rick, I really appreciate it!

Aaron Longnion
Posted by aqlong on December 18, 2006 at 9:46 AM

Post a comment (login required)