House of Fusion
Search over 2,500 ColdFusion resources here
  
Home of the ColdFusion Community

Mailing Lists
Home /  Groups /  ColdFusion Talk (CF-Talk)

Interesting results with setting variables - cfset vs using a cfscript block

  << Previous Post |  RSS |  Sort Oldest First |  Sort Latest First |  Subscribe to this Group Next >> 
thanks dominic,
Larry Lyons
07/02/08 02:54 P
Note that
Claude Schneegans
07/02/08 10:32 A
Stephane Vantroyen wrote:
Pete Jordan
07/02/08 12:22 P
Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Larry Lyons
07/02/2008 08:16 AM

I had originally posted this on CF-Community, but I thought that these results were interesting enough that others would like to see them. I got into a discussion at my new job about creating variables by the cfset tag or via a variable assignment using cfscript. The consensus has been that when setting large blocks of variables using cfscript is faster than cfset. The reason that tags are slower is that when the template JITs, it adds extra libraries into the class. When you use cfscript,it uses fewer cftag libraries mentioned as Java import namespaces because the cfscript code is closer to the native Java and therefore requires less overhead (at least according to some blog postings I've read). Also, the tag named import also contains functionality the code may not be using. Therefore, you have thinner JITs and faster code. Or so I thought. Here's some code I wrote to test this, not that this is over a million iterations, a pretty unreal situation ordinarily: <cfset iterations = 1000000/> <cfscript>    start1 = getTickCount();    result=0;    for(i=0;i lte #iterations#;i=i+1) {       result=result+i;    }    end1 = getTickCount(); </cfscript> <cfset start2 = getTickCount() /> <cfset result2 = 0 /> <cfloop from="1" to="#iterations#" index="j">    <cfset result2=result2+j /> </cfloop> <cfset end2 = getTickCount() /> <fieldset> <legend>CFSCRIPT</legend>    <p><cfoutput>#result#</cfoutput><br/>    <cfoutput>Execution Time: #end1 - start1#ms</cfoutput></p> </fieldset> <fieldset><legend>CFSET</legend>    <p><cfoutput>#result2#</cfoutput><br/>    <cfoutput>Execution Time: #end2 - start2#ms</cfoutput></p> </fieldset> Here are the results: CFSCRIPT total: 500000500000 Execution Time: 1472ms CFSET total: 500000500000 Execution Time: 947ms So am I missing something here? This was on a Windows XP box with service pack 2 installed (dell optiplex dual core with 2 gigs of ram). Its running the dev edition of CFMX 7.02 with cumulative hotfix 3 installed. I got similar results using Open BlueDragon on a MacBook with OSX 10.5.3. Generally these results go against what's commonly thought. However given that there was only about a half second difference over a million iterations, is it really an important difference?

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Dominic Watson
07/02/2008 09:05 AM

I ran your code a few times on my machine (XP Pro) and got varying results; sometimes the cfscript was faster, sometimes cfset. Try this code out instead, it performs a single test on both cfscript and cfset for each iteration and records a tally of the time taken for each - this spreads the test and reduces variance due to the varying state of the computers processor, etc. So far I always have cfscript as slightly faster: <cfset iterations = 1000000/> <cfset scriptTotal = 0> <cfset cfsetTotal = 0> <cfset scriptResult = 0 /> <cfset cfsetResult = 0 /> <cfloop from="1" to="#iterations#" index="i">     <cfset tick = getTickCount()>   <cfset cfsetResult = cfsetResult + i />   <cfset cfsetTotal = cfsetTotal + (getTickCount() - tick)>      <cfscript>      tick = getTickCount();      scriptResult = scriptResult + i;      scriptTotal = scriptTotal + (getTickCount() - tick);   </cfscript> </cfloop> <fieldset>   <legend>CFSCRIPT</legend>   <p><cfoutput>#scriptResult#</cfoutput><br/>   <cfoutput>Execution Time: #scriptTotal#ms</cfoutput></p> </fieldset> <fieldset>   <legend>CFSET</legend>   <p><cfoutput>#cfsetResult#</cfoutput><br/>   <cfoutput>Execution Time: #cfsetTotal#ms</cfoutput></p> </fieldset> Dominic ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Larry Lyons
07/02/2008 02:54 PM

thanks dominic, I ran your code and the results between setting variable blocks with cfscript or using cfset varied from trial to trial. Some times cfscript was faster, other times cfset. Anyhow in a nutshell it confirms my initial idea, that it isn't really a major issue. In the end it depends on personal style, readability and other user factors as to which is preferred when setting large blocks of variables.. regards, larry ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Dominic Watson
07/02/2008 03:03 PM

>In the end it depends on personal style, readability and other user factors as to which is preferred when setting large blocks of variables.. Yes absolutely ;) Dominic ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Claude Schneegans
07/02/2008 10:32 AM

Note that 1º according to the docs, GetTickCount() returns "a string representation of the system time," not the time spent on the CF code. The CF server is running in a time shared environment, so the system time is not really representative of the execution time. 2º There is a huge difference between what fellow developers think how things should be logically in theory and how they really are in practice ;-) -- _______________________________________ REUSE CODE! Use custom tags; See http://www.contentbox.com/claude/customtags/tagstore.cfm (Please send any spam to this address: piegeacon@internetique.com) Thanks.

Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Stephane Vantroyen
07/02/2008 08:46 AM

You should read this, quite interesting (CF8 vs CF7 etc) : http://neilmiddleton.com/2007/07/01/cf-8-and-performance-comparisons/ Regards, Stephane

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Pete Jordan
07/02/2008 12:22 PM

Stephane Vantroyen wrote: > You should read this, quite interesting (CF8 vs CF7 etc) : http://neilmiddleton.com/2007/07/01/cf-8-and-performance-comparisons/ My guess is that a lot (most?) of the runtime difference between CF7 and 8 is down to the JVM; Java 6 is faster than 5 and a *lot* faster than 4. FWIW, we're running both CF7 and 8. and have run 7 with (Sun) Java 6 pretty much since it released, without any problems at all: I'd guess that macradobe warnings on that are mostly to cover themselves. -- Regards, Pete Jordan Horus Web Engineering Ltd http://www.webhorus.net/ phone: +44 1275 543971 mobile: +44 7973 725120


<< Previous Thread Today's Threads Next Thread >>

Search cf-talk

April 20, 2014

<<   <   Today   >   >>
Su Mo Tu We Th Fr Sa
     1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30       

Designer, Developer and mobile workflow conference