ColdFusion performance: compare to 0 in cfloop

Written on 6 December 2014, 11:31am

Tagged with: ,

The piece of code below proves that in ColdFusion comparing to 0 and using a negative step is more efficient than the traditional way of looping.

<cfset LOOPS1 = 5>
<cfset LOOPS2 = 100*1000>

<cfset STEPS = 10>

<cfloop index="k" to="0" from="#STEPS#" step="-1">
	<cfset start = GetTickCount()>
	<cfloop index='i' from='0' to='#LOOPS1#' step='1'>
		<cfloop index='j' from='0' to='#LOOPS2#' step='1'>
		</cfloop>
	</cfloop>
	<cfset exectime1 = GetTickCount() - start>



	<cfset start = GetTickCount()>
	<cfloop index='i' from='#LOOPS1#' to='0' step='-1'>
		<cfloop index='j' from='#LOOPS2#' to='0' step='-1'>
		</cfloop>
	</cfloop>
	<cfset exectime2 = GetTickCount() - start>

	<cfset diff = exectime1 - exectime2>
	<cfoutput>#diff#</cfoutput><br><cfflush>
</cfloop>

Ran on Railo 4.2 produced the following output:

123
20
33
37
17
19
9
12
22
47
11

Leave a response