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>
You are not logged in, so your subscription status for this entry is unknown. You can login or register here.
http://www.cflib.org/udf.cfm?ID=1197
HTH
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
thanks!
http://www.opensourcecf.com/1/2006/12/UDF-to-generate-File-Drops-in-CSV-Excel-and-Tab-delimited-formats.cfm
Aaron Longnion
Post a comment (login required)
