You're right, it works better with more code between the gettickcount()
statements...
My results:
method iterations min max avg sum
CFIF 100 0 20 8 751
CFSWITCH 100 0 11 7 641
CFTRY 100 70 180 87 8692
Ran it a few times -- it varies some but not by huge amounts... cftry is
always up there -- dunno if using a single <cfcatch> instead of multiples
would change that any ... haven't tested it... would probably also need to
try it with more elements for instance, a switch-case statement with 30
items instead of 3 to know what effect the size of a switch statement and or
an if-else ladder might have on these comparisons.
But I'm too lazy to rewrite it for that. ;P
My suspiscion is that as the size of the if-else ladder increases, the time
to process increases significantly more than for a switch-case statement.
I'd be inclined to suspect that the try-catch block inflates slower than the
if-else ladder also, but because it starts at about 10x the processing time
required for the 3-member if-else statement in this example, even if it does
inflate slower, you'd have to have well more elseif statements in your
ladder than I've _ever_ used for it to be more efficient.
In the end I maintain that in the 3 or 4 instances where I'm using it for
flow-control any other equivalent code I could imagine would have been a
horrible, illegible mess, so it's primarily for development purposes _ and _
these are in low-traffic admin pages with the overhead of the try-catch
block being mitigated with <cfflush> to update a progress bar for the
user...
So it shouldn't be used for flow control, accept maybe very rarely if you
have an exceptionally good reason to use it and you know you can manage the
overhead. :)
<cfparam name="url.iterations" type="numeric" default="100">
<cfparam name="url.iteration2" type="numeric" default="100">
<cfset request.times = structnew()>
<cfset request.times.cftry = ArrayNew(1)>
<cfset request.times.cfswitch = arraynew(1)>
<cfset request.times.cfif = arraynew(1)>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>Try-Catch Time Test</title></head><body>
<cfloop index="iteration" from="1" to="#url.iterations#">
<cfset starttime = gettickcount()>
<cfloop index="iteration2" from="1" to="#url.iteration2#">
<cfset candrive = false>
<cfset candrink = false>
<cfset myage = RandRange(1,35)>
<cfswitch expression="#myage#">
<cfcase value="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15"
delimiters=",">
<cfset message="this is a tyke">
</cfcase>
<cfcase value="16,17,18,19,20" delimiters=",">
<cfset candrive = true>
<cfset message="this is a teen">
</cfcase>
<cfdefaultcase>
<cfset candrink = 1>
<cfset message="this is an adult">
</cfdefaultcase>
</cfswitch>
</cfloop>
<cfset executiontime = gettickcount() - starttime>
<cfset temp = arrayappend(request.times.cfswitch,executiontime)>
<cfset starttime = gettickcount()>
<cfloop index="iteration2" from="1" to="#url.iteration2#">
<cfset candrive = false>
<cfset candrink = false>
<cfset myage = RandRange(1,35)>
<cftry>
<cfif myage lt 16><cfthrow type="tyke"></cfif>
<cfset candrive = true>
<cfif myage lt 21><cfthrow type="teen"></cfif>
<cfset candrink = true>
<cfset message="this is an adult">
<cfcatch type="tyke"><cfset message="this is a tyke"></cfcatch>
<cfcatch type="teen"><cfset message="this is a teen"></cfcatch>
</cftry>
</cfloop>
<cfset executiontime = gettickcount() - starttime>
<cfset temp = arrayappend(request.times.cftry,executiontime)>
<cfset starttime = gettickcount()>
<cfloop index="iteration2" from="1" to="#url.iteration2#">
<cfset candrive = false>
<cfset candrink = false>
<cfset myage = RandRange(1,35)>
<cfif myage lt 16>
<cfset message = "this is a tyke">
<cfelseif myage lt 21>
<cfset message = "this is a teen">
<cfset candrive = true>
<cfelse>
<cfset message="this is an adult">
<cfset candrink = true>
</cfif>
</cfloop>
<cfset executiontime = gettickcount() - starttime>
<cfset temp = arrayappend(request.times.cfif,executiontime)>
</cfloop>
<cfoutput>
<table>
<tr><td>method</td><td>iterations</td><td>min</td><td>max</td><td>avg</td><
td>sum</td></tr>
<cfloop item="method" collection="#request.times#">
<tr><td>#method#</td>
<td>#arraylen(request.times[method])#</td>
<td>#ceiling(arraymin(request.times[method]))#</td>
<td>#ceiling(arraymax(request.times[method]))#</td>
<td>#ceiling(arrayavg(request.times[method]))#</td>
<td>#ceiling(arraysum(request.times[method]))#</td>
</tr>
</cfloop>
</table>
</cfoutput>
</body>
Isaac
Certified Advanced ColdFusion 5 Developer
www.turnkey.to
954-776-0046
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4
Subscription: http://www.houseoffusion.com/index.cfm?sidebar=lists&body=lists/cf_talk
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
Get the mailserver that powers this list at http://www.coolfusion.com