Tuesday, 10 September 2013

Is it inefficient to use implicit array creation (a.k.a. shorthand or literal notation) in loops or function parameters?

Is it inefficient to use implicit array creation (a.k.a. shorthand or
literal notation) in loops or function parameters?

(This is sort of a follow-up to my previous question about structs)
Would this:
<cfset myArray = ArrayNew(1)>
<cfloop query="myQuery">
<cfset myArray[1] = queryCol1>
<cfset myArray[2] = queryCol2>
<cfset myArray[3] = queryCol3>
<cfset funcionWithArrayParam(myArray)>
</cfloop>
... use system resources more efficiently than this:
<cfloop query="myQuery">
<cfset functionWithArrayParam([queryCol1, queryCol2, queryCol3])>
</cfloop>
?
And to keep the focus narrow, assume that the array in the first example
serves no purpose other than to feed the function. Also, assume that the
query and/or column data are large enough to make efficiency a worthwhile
consideration.
The second method is tempting for its compactness, but my concern is that
I'm not sure how many new arrays it's creating on each iteration. The
first method creates one, I believe, due to the fact that arrays are
passed by value in CF. But does that mean the second method creates two
(one implicit, and one param)?
It would also be interesting to know how shorthand notation compares,
performance-wise, to multiple cfset statements for populating an array. I
realize it's not apples-to-apples since the former also creates the array,
but still... I suppose I could easily test this myself.

No comments:

Post a Comment