Skip to content

Instantly share code, notes, and snippets.

@aliaspooryorik
Created December 8, 2016 12:12
Show Gist options
  • Save aliaspooryorik/d9a949955bfb7ce3f3af121ca143305a to your computer and use it in GitHub Desktop.
Save aliaspooryorik/d9a949955bfb7ce3f3af121ca143305a to your computer and use it in GitHub Desktop.
Speed test - string concat
<cfscript>
iterations = 1000;
sampletext = RepeatString("Lorem ipsum dolor sit amet, consectetuer adipiscing elit.", 100);
result = [];
// Method 1
i = 0;
startTime = getTickCount();
finalString = "";
while ( i < iterations ) {
finalString = finalString & sampletext;
i++;
}
endTime = getTickCount();
totalTime = endTime-startTime;
ArrayAppend( result, { label="concat", ms=totalTime } );
// Method 2
i = 0;
startTime = getTickCount();
finalString = "";
while ( i < iterations ) {
finalString &= sampletext;
i++;
}
endTime = getTickCount();
totalTime = endTime-startTime;
ArrayAppend( result, { label="concat shorthand", ms=totalTime } );
// Method 3
i = 0;
startTime = getTickCount();
finalString = "";
while ( i < iterations ) {
finalString = ListAppend( finalString, sampletext, Chr(1) );
i++;
}
finalString = ListChangeDelims( finalString, "", Chr(1) );
endTime = getTickCount();
totalTime = endTime-startTime;
ArrayAppend( result, { label="ListAppend", ms=totalTime } );
// Method 4
i = 0;
startTime = getTickCount();
finalString = [];
while ( i < iterations ) {
ArrayAppend( finalString, sampletext );
i++;
}
finalString = ArrayToList( finalString, "" );
endTime = getTickCount();
totalTime = endTime-startTime;
ArrayAppend( result, { label="ArrayAppend", ms=totalTime } );
// Method 5
i = 0;
startTime = getTickCount();
finalString = [];
savecontent variable="finalString"{
while ( i < iterations ) {
WriteOutput( sampletext );
i++;
}
}
endTime = getTickCount();
totalTime = endTime-startTime;
ArrayAppend( result, { label="cfsavecontent", ms=totalTime } );
// Method 6
i = 0;
startTime = getTickCount();
finalString = CreateObject( "java", "java.lang.StringBuffer" ).init();
savecontent variable="finalString"{
while ( i < iterations ) {
finalString.append( sampletext );
i++;
}
}
finalString = finalString.toString();
endTime = getTickCount();
totalTime = endTime-startTime;
ArrayAppend( result, { label="StringBuffer", ms=totalTime } );
// Method 7
i = 0;
startTime = getTickCount();
finalString = CreateObject( "java", "java.lang.StringBuilder" ).init();
savecontent variable="finalString"{
while ( i < iterations ) {
finalString.append( sampletext );
i++;
}
}
finalString = finalString.toString();
endTime = getTickCount();
totalTime = endTime-startTime;
ArrayAppend( result, { label="StringBuilder", ms=totalTime } );
writeDump(result);
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment