Now, I am sure I will catch some flack for this performance test, but until someone actually gives me CONSTRUCTIVE criticism on performance testing, this is the only way I know how to do it.
That said, I was chilling in my bed last night watching a Family Guy rerun when I started to think about ColdFusion frameworks. Nothing in particular, just frameworks in general. This got me thinking about CFSwitch statements. I started to wonder if the order of CFCase statements mattered for performance. I know that in a CFIF/CFELSEIF/CFELSE statement, you want to put the more commonly used statements first (so that less conditions need to be evaluated), but I wondered if anything like that existed for CFSwitch / CFCase statements?
To test, I programmatically built a very large CFSwitch statement into its own file, cases.cfm:
<cfoutput> <cfsavecontent variable="strCode"> [[cfswitch expression="##strValue##"><cfloop index="i" from="#Asc( 'A' )#" to="#Asc( 'Z' )#"> <cfloop index="j" from="1" to="70" >[[cfcase value="#Chr( i )##j#"> [[cfset WriteOutput( "#Chr( i )##j#" ) /> [[/cfcase> </cfloop></cfloop>[[/cfswitch> </cfsavecontent> </cfoutput> <cffile action="WRITE" file="#ExpandPath( './cases.cfm' )#" output="#strCode.ReplaceAll( '\[\[', '<' ).Trim()#" />
Sorry for the crap readability, but I was trying to make the file as small as possible. This makes case statements that start with a letter (A-Z) and for each letter makes cases 1 to 70. This comes out to be 1820 case statements in the switch in alphabetical ASC order.
Then, I basically just loop over that a few times picking random cases. In my first loop I make sure to pick only cases that start with "A". These should be the first ones in the CFSwitch statement. In the second loop I make sure to pick only cases that start with "Z". These should be the last ones in the CFSwitch statement:
<!--- Only pick early cases. ---> <cftimer label="A - Cases" type="outline"> <cfloop index="intI" from="1" to="50" step="1"> <!--- Get a random case to test. ---> <cfset strValue = ( "A" & RandRange( 1, 50 ) ) /> <!--- Include the cases. ---> <cfinclude template="cases.cfm" /> </cfloop> </cftimer> <!--- Only pick later cases. ---> <cftimer label="Z - Cases" type="outline"> <cfloop index="intI" from="1" to="50" step="1"> <!--- Get a random case to test. ---> <cfset strValue = ( "Z" & RandRange( 1, 50 ) ) /> <!--- Include the cases. ---> <cfinclude template="cases.cfm" /> </cfloop> </cftimer>
What I found was that there was absolutely no noticeable trend. Sometimes one was faster, sometimes the other was faster. That's pretty cool to know. And, this was for a HUGE number of cases. On a small case set, it must be even faster. Man, I love ColdFusion, it's so freakin' sweet! So, in conclusion, CFCase statement order does not seem to have any affect on look-up times. I guess this all comes down to how the language eventually compiles down into machine code. I did take a class in that, or something similar (SPARC Architecture), but that was an eon ago and all I remember is understanding that Programming Languages were built so we didn't have to deal with it :)
Want to use code from this post? Check out the license.