Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CFSwitch - is order important

Status
Not open for further replies.

DrumAt5280

Technical User
Joined
Sep 8, 2003
Messages
194
Location
US
If i am using CFSwitch statement and have about 40 CFCase's within the switch. Would the switch statement theoretically run faster if i place the most frequently used cfcase statements at the top?

I guess what i am asking is when CF processes the switch statement and then it hits the matching cfcase statement does it stop processing the rest of the statement and move on?

I just trying to learn CF best practices.

 
I seem to remember reading that was the reason cfswitch is faster than cfif,cfelseif,cfelse, because it doesn't have to read each condition. I can't remember if it has to loop through the cfcase values or if it just jumps to it. if it loops i would imagine the top being most frequent result would help. if it can just jump to the cfcase the order wouldn't matter much.



Beware of programmers who carry screwdrivers.
 
I should add i did some searching to see if it had to loop over the cfcase values or just jump to the cfcase value but couldn't find anything that supported either method.

Beware of programmers who carry screwdrivers.
 
I think i have a way to test this.

If i place a cfcase high up in the switch statement and one at the bottom with the same matching value. with the top one with a <cfset position = high> and bottom with a <cfset position = low>. If the resulting include has a position high resulting then i know CF aborts after it finds the first one.

Would this be a valid test?
 
The test didn't work, because cf doesn't like duplicates.

I would still like to know the answer.
 
ok try this.
make a huge cfswitch/case block 100 or so cases. (use copy and paste a lot it'll help )

once you create the first one copy the whole thing below it.
set it up so the first one finds the value on top, the second block finds the value at the bottom.

use the suggestion i posted in your last thread
thread232-1043734
around each block.

the block with the smaller number (if there is one) is the fastest.



Beware of programmers who carry screwdrivers.
 
I tried that and they all seem to come in a 0ms. I guess i did the test right. If this is an accurate test, then i don't need to worry about this at all.
 
There was an article on the Macromedia site (or should i now say the Adobe site!) that talked about the cfswitch/case statement. It said something along the lines of, the most commonly called case's should be positioned at the top of the statement to ensure the efficieny of the statement.

To be honest I have never noticed any speed differences to be honest and we have got some quite large switch statements

Hope this helps!

Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top