Last active
December 26, 2015 19:59
-
-
Save JamoCA/7205199 to your computer and use it in GitHub Desktop.
ColdFusion looping comparison script - CFScript versus CFML. Based on information available at http://duncan99.wordpress.com/2012/10/18/coldfusion-cfloop-versus-cfscript-for-loop-performance/ I tested this because some functions seemed slower when migrating CFML to CFScript.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>CFLoop Test - CFScript versus CFML</title> | |
</head> | |
<body> | |
<h1>CFLoop Test - CFScript versus CFML</h1> | |
<cfset testresults = arraynew(2)> | |
<cfset myarray = arraynew(1)> | |
<cfloop index="i" from="1" to="1000000"><cfset arrayappend(myarray, "")></cfloop> | |
<cfoutput> | |
<h2>Looping over #NumberFormat(ArrayLen(MyArray))# array elements</h2> | |
</cfoutput> | |
<cfsilent> | |
<cfscript> | |
function loopit(a){ | |
arrayEach(a,function(){}); | |
} | |
</cfscript> | |
<cfloop index="j" from="1" to="10"> | |
<cfscript> | |
tick = GetTickCount(); | |
for (i=1; i <= arraylen(myarray); i++) {} | |
</cfscript> | |
<cfset testresults[1][j] = numberformat(gettickcount()-tick)> | |
<cfscript> | |
tick = GetTickCount(); | |
len = arrayLen(myArray); | |
for (i=1; i <= len; i++) {} | |
</cfscript> | |
<cfset testresults[2][j] = numberformat(gettickcount()-tick)> | |
<cfscript> | |
tick = GetTickCount(); | |
for (item in myArray) {} | |
</cfscript> | |
<cfset testresults[3][j] = numberformat(gettickcount()-tick)> | |
<cfscript> | |
tick = GetTickCount(); | |
arrayEach(myArray,function(){}); | |
</cfscript> | |
<cfset testresults[4][j] = numberformat(gettickcount()-tick)> | |
<cfscript> | |
tick = GetTickCount(); | |
loopit(myArray); | |
</cfscript> | |
<cfset testresults[5][j] = numberformat(gettickcount()-tick)> | |
<cfset tick = gettickcount()> | |
<cfloop index="i" from="1" to="#arrayLen(myArray)#"></cfloop> | |
<cfset testresults[6][j] = numberformat(gettickcount()-tick)> | |
<cfset tick = gettickcount()> | |
<cfset len = arraylen(myarray)> | |
<cfloop index="i" from="1" to="#len#"></cfloop> | |
<cfset testresults[7][j] = numberformat(gettickcount()-tick)> | |
<cfset tick = gettickcount()> | |
<cfloop array="#MyArray#" index="i"></cfloop> | |
<cfset testresults[8][j] = numberformat(gettickcount()-tick)> | |
</cfloop> | |
</cfsilent> | |
<style> | |
#myTable tbody td {text-align:right;} | |
#myTable tfoot td {text-align:right; font-weight:bold;} | |
</style> | |
<table id="myTable" border=1 cellspacing=0 cellpadding=1> | |
<thead> | |
<tr> | |
<th>Iteration</th> | |
<th>CFScript (arrayLen)</th> | |
<th>CFScript (var'ed)</th> | |
<th>CFScript (for item in)</th> | |
<th>CFScript (arrayEach)</th> | |
<th>CFScript (arrayEach UDF)</th> | |
<th>CFML (arrayLen)</th> | |
<th>CFML (var'ed)</th> | |
<th>CFML (array)</th> | |
</tr> | |
</thead> | |
<cfoutput> | |
<tfoot> | |
<tr><td>Avg</td><cfloop from="1" to="8" index="i"><td>#ArrayAvg(TestResults[i])#</td></cfloop></tr> | |
<tr><td>Sum</td><cfloop from="1" to="8" index="i"><td>#ArraySum(TestResults[i])#</td></cfloop></tr> | |
<tr><td>Min</td><cfloop from="1" to="8" index="i"><td>#ArrayMin(TestResults[i])#</td></cfloop></tr> | |
<tr><td>Max</td><cfloop from="1" to="8" index="i"><td>#ArrayMax(TestResults[i])#</td></cfloop></tr> | |
</tfoot> | |
<tbody> | |
<cfloop from="1" to="10" index="J"> | |
<tr><td>#J#</td><cfloop from="1" to="8" index="i"><td>#TestResults[i][J]#</td></cfloop></tr> | |
</cfloop> | |
</table></cfoutput> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment