Aha. I know the difference!
My first batch of tests was testing something quite specific, and the
"if true" and "if false" expressions we static text, eg:
iif(experience gt 25, de("sean"), de("unworthy"))
So I guess that circumvents the evaluate() issue.
Here's the code:
<cfparam name="url.iterations" default="1">
iif()
<cf_timeit>
<cfloop index="i" from="1" to="#url.iterations#">
<cfset x = iif(randRange(1,2) eq 1, de('heads'), de('tails'))>
</cfloop>
</cf_timeit>
<cfif>/<else>
<cf_timeit>
<cfloop index="i" from="1" to="#url.iterations#">
<cfif randRange(1,2) eq 1>
<cfset x = 'heads'>
<cfelse>
<cfset x = 'tails'>
</cfif>
</cfloop>
</cf_timeit>
<cfif>
<cf_timeit>
<cfloop index="i" from="1" to="#url.iterations#">
<cfset x = 'tails'>
<cfif randRange(1,2) eq 1>
<cfset x = 'heads'>
</cfif>
</cfloop>
</cf_timeit>
<switch>
<cf_timeit>
<cfloop index="i" from="1" to="#url.iterations#">
<cfswitch expression="#randRange(1,2)#">
<cfcase value="1">
<cfset x = 'heads'>
</cfcase>
<cfdefaultcase>
<cfset x = 'tails'>
</cfdefaultcase>
</cfswitch>
</cfloop>
</cf_timeit>
switch()
<cf_timeit>
<cfloop index="i" from="1" to="#url.iterations#">
<cfscript>
switch (randRange(1,2)){
case 1: {
x = 'heads';
break;
}
default: {
x = 'tails';
}
}
</cfscript>
</cfloop>
</cf_timeit>
Typical results on CFMX7 (100000 iterations):
iif() Runtime: 234ms
<cfif>/<else> Runtime: 670ms
<cfif> Runtime: 467ms
<switch> Runtime: 483ms
switch() Runtime: 608ms
Typical results on CF5 (10000 iterations; slower machine):
iif() Runtime: 1122ms
<cfif>/<else> Runtime: 340ms
<cfif> Runtime: 391ms
<switch> Runtime: 370ms
switch() Runtime: 391ms
The notable thing here is the proportional difference between
performance from CF5 to CFMX7. iif() USED to be a monster, now there's
not much in it. And indeed, in this example, is almost always trivially
faster on CFMX7.
But I've altered the test to include executable code in the iif():
<cfparam name="url.iterations" default="1">
<cfscript>
function someAction(i){
return arguments.i;
}
function someOtherAction(i){
return arguments.i;
}
iMin = -2147483648;
iMax = 2147483647;
</cfscript>
iif()
<cf_timeit>
<cfloop index="i" from="1" to="#url.iterations#">
<cfset i = randRange(iMin, iMax)>
<cfset x = iif(i gt 0, "someAction(i)", "someOtherAction(i)")>
</cfloop>
</cf_timeit>
<cfif>/<else>
<cf_timeit>
<cfloop index="i" from="1" to="#url.iterations#">
<cfset i = randRange(iMin, iMax)>
<cfif i gt 0>
<cfset x = someAction(i)>
<cfelse>
<cfset x = someOtherAction(i)>
</cfif>
</cfloop>
</cf_timeit>
<cfif>
<cf_timeit>
<cfloop index="i" from="1" to="#url.iterations#">
<cfset i = randRange(iMin, iMax)>
<cfset x = someOtherAction(i)>
<cfif i gt 0>
<cfset x = someAction(i)>
</cfif>
</cfloop>
</cf_timeit>
<switch>
<cf_timeit>
<cfloop index="i" from="1" to="#url.iterations#">
<cfset i = randRange(iMin, iMax)>
<cfswitch expression="#i gt 0#">
<cfcase value="yes">
<cfset x = someAction(i)>
</cfcase>
<cfcase value="no">
<cfset x = someOtherAction(i)>
</cfcase>
</cfswitch>
</cfloop>
</cf_timeit>
switch()
<cf_timeit>
<cfloop index="i" from="1" to="#url.iterations#">
<cfscript>
i = randRange(iMin, iMax);
switch (i gt 0){
case "Yes": {
x = someAction(i);
break;
}
case "No": {
x = someOtherAction(i);
}
}
</cfscript>
</cfloop>
</cf_timeit>
Typical results on CFMX7 (100000 iterations):
iif() Runtime: 1496ms
<cfif>/<else> Runtime: 1465ms
<cfif> Runtime: 1138ms
<switch> Runtime: 1605ms
switch() Runtime: 1341ms
Typical results on CF5 (10000 iterations; slower machine):
iif() Runtime: 1822ms
<cfif>/<else> Runtime: 992ms
<cfif> Runtime: 981ms
<switch> Runtime: 1112ms
switch() Runtime: 1131ms
(PS: code had to be changed slightly for CF5)
To be honest, in about ten tests I did on CFMX7, the iif() was still
faster 4/10 times. But, yeah, slower the other six. The margin was
quite trivial: +/-200ms seemed to be the greatest difference. And
that's over 100000 iterations.
The bottom line is that it might have been a good idea to avoid iif() in
CF5, but perhaps not really worth worrying about in CFMX7?
I dunno. *I'm* not going to lose sleep over it, anyhow.
--
Adam
This email contains confidential information. If you are not the intended
recipient of this email, please notify Straker Interactive and delete the
email. You are not entitled to use it in any way.
----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email to
[email protected] with the words 'unsubscribe cfcdev' as the subject of the
email.
CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting
(www.cfxhosting.com).
CFCDev is supported by New Atlanta, makers of BlueDragon
http://www.newatlanta.com/products/bluedragon/index.cfm
An archive of the CFCDev list is available at
www.mail-archive.com/[email protected]