Array Looping Much Faster Than Query Looping

Posted September 10, 2006 at 10:31 AM

Tags: ColdFusion

I pretty much guessed that looping over an array in ColdFusion would be faster than looping over a query in ColdFusion. But, to me, looping over a query is SOOO much cleaner looking (only when using the CFLoop tag, not in CFScript tags) than looping over an array that I ran this test in hopes that magically a query loop would be faster. I was thinking about this in terms of creating static collections of data, such as a list of states or a list of countries. Who hasn't had to loop over those at some time?

The following test creates a one-dimensional array and query and just tests how fast it can be iterated over. A query can be looped over in a query loop OR in an index loop, so I tested both of those. But first, I created the data sets:

 Launch code in new window » Download code as text file »

  • <!--- Set the data size. --->
  • <cfset intDataSize = 10000 />
  •  
  •  
  • <!--- Create a data array. --->
  • <cfset arrGirls = ArrayNew( 1 ) />
  •  
  • <!--- Resize the array. --->
  • <cfset ArrayResize( arrGirls, intDataSize ) />
  •  
  • <!--- Add data to the array. --->
  • <cfloop index="intI" from="1" to="#intDataSize#" step="1">
  •  
  • <!--- Set data item. --->
  • <cfset arrGirls[ intI ] = ListGetAt(
  • "Libby,Marry Kate,Ashley,Azure,Francis,Lori,Alex",
  • RandRange( 1, 7 )
  • ) />
  •  
  • </cfloop>
  •  
  •  
  • <!--- Create the data query. --->
  • <cfset qGirls = QueryNew( "name" ) />
  •  
  • <!--- Add rows to the query. --->
  • <cfset QueryAddRow( qGirls, intDataSize ) />
  •  
  • <!--- Add data to the array. --->
  • <cfloop index="intI" from="1" to="#intDataSize#" step="1">
  •  
  • <!--- Set data item. --->
  • <cfset qGirls[ "name" ][ intI ] = ListGetAt(
  • "Libby,Marry Kate,Ashley,Azure,Francis,Lori,Alex",
  • RandRange( 1, 7 )
  • ) />
  •  
  • </cfloop>

As you can see from above, I am creating a data set that is 10,000 items long. Then, I tested how long each type of loop would take. For the looping, all I did was output the data item value:

 Launch code in new window » Download code as text file »

  • <!--- Test how long the array loop takes. --->
  • <cftimer label="Array Loop" type="outline">
  •  
  • <!--- Loop over array using an index loop. --->
  • <cfloop index="intI" from="1" to="#ArrayLen( arrGirls )#" step="1">
  • #arrGirls[ intI ]#
  • </cfloop>
  •  
  • </cftimer>
  •  
  •  
  • <!--- Test how long the query loop takes. --->
  • <cftimer label="Query Loop" type="outline">
  •  
  • <!--- Loop over the query. --->
  • <cfloop query="qGirls">
  • #qGirls.name#
  • </cfloop>
  •  
  • </cftimer>
  •  
  •  
  • <!--- Test how long the query loop takes when used like an array. --->
  • <cftimer label="Query Loop (as Array)" type="outline">
  •  
  • <!--- Loop over the query using an index loop. --->
  • <cfloop index="intI" from="1" to="#qGirls.RecordCount#" step="1">
  • #qGirls[ "name" ][ intI ]#
  • </cfloop>
  •  
  • </cftimer>

Not suprisingly, the array loop was MUCH faster than either of the query loops. The speed results on average where:

  • Array: 31ms - 90ms
  • Query Loop: 3140ms - 3234ms
  • Query Loop (as Array): 2344ms - 3109ms

As you can see, the array loop is like 50 times faster than the query loop. Keep in mind that this is over 10,000 iterations, but still! That IS much faster. Using the query as a multidimensional array ([column][row]) had some speed improvements, but was still tens of times slower than the array index.

So again, nothing special here. That was pretty much was what I expected (though not quite such a large performance difference). But, I blogged earlier about the array iterator, and was curious how that would perform against the query loop. The iterator loop still looks nicer to me.

 Launch code in new window » Download code as text file »

  • <!--- Test how long the array loop takes with the iterator. --->
  • <cftimer label="Array Loop (as Iterator)" type="outline">
  •  
  • <!--- Get the array iterator. --->
  • <cfset objIterator = arrGirls.Iterator() />
  •  
  • <!--- Loop over the array while we have items. --->
  • <cfloop condition="objIterator.HasNext()">
  • #objIterator.Next()#
  • </cfloop>
  •  
  • </cftimer>

Suprisingly, even with the added method calls and iterative condition evaluation, the array iterator is still VERY fast. On average it performed as such:

Array Iterator: 78ms - 93ms

As you can see, it performs comparable to the array index loop, but at the slow end of the spectrum. It's still like 50 times faster than the query loop. To me, it just looks very elegant. The one issue I have with it is that you have to create the iterator before you go into the loop. This doesn't look as nice as just looping. So, in a select box output, I might not opt for the iterator (as I am very anal about how my code looks), but if I was in a CFScript tag, I would definitely choose the array iterator over the array index loop as the code looks nice, is more "readable," and is less bulky.

Download Code Snippet ZIP File

Comments (0)  |  Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Reader Comments

There are no comments posted for this web log entry.


Post Comment  |  Ask Ben


Home   |   Web Log   |   ColdFusion   |   Projects   |   Resume   |   Job Form   |   Search   |   Contact
Epicenter Consulting - Custom Software Solutions for Business Evolution HostMySite.com - The Leader In ColdFusion Hosting