Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.
-
cjeris #1
Speed of component methods vs functions
Macromedia recommends (Developer's Guide, chapter 13, "Managing the application
with Application.cfc") that application-wide utility functions be placed in a
CFC (specifically, one other than Application.cfc). I did some speed tests and
it seems to me that a CFC method call is at least 15 times slower than a
function call. The (very simple-minded) tests are described below. If this is
true, is there a way to get the speed of function calls while still retaining
component-style organization? If I follow the bad old CF5 style and CFINCLUDE
a template containing my functions on every call, is that bad for other
reasons? (in particular, does it force recompilation every time?)
I did these tests on my PC which is a 3.2 GHz P4 with 1GB RAM running Windows
2000 SP4 and CFMX7 Developer Edition (7,0,0,91690) served via the internal JRun
web server. There are several other big things running but everything normally
fits into RAM and nobody else is a big CPU consumer. I know that these are not
really controlled benchmarks, but the disparity is just huge.
Here's the code:
<!--- bar.cfc : this contains the toy component --->
<CFCOMPONENT>
<CFFUNCTION name="render_date_short" returntype="string" output="false">
<CFARGUMENT name="d" type="date" required="true">
<CFRETURN DateFormat(d, 'yyyy-mm-dd')>
</CFFUNCTION>
</CFCOMPONENT>
<!--- foo1.cfm: test via CFINVOKE without instantiating --->
<CFSET the_now = Now()>
<CFLOOP index="i" from="1" to="15000">
<CFINVOKE component="bar" method="render_date_short" d="#the_now#"
returnvariable="ignored">
</CFLOOP>
<!--- foo2.cfm: test via CFINVOKE _and_ instantiating --->
<CFSET the_now = Now()>
<CFOBJECT component="bar" name="barcomp">
<CFLOOP index="i" from="1" to="15000">
<CFINVOKE component="#barcomp#" method="render_date_short" d="#the_now#"
returnvariable="ignored">
</CFLOOP>
<!--- foo3.cfm: test via method call syntax --->
<CFSET the_now = Now()>
<CFOBJECT component="bar" name="barcomp">
<CFLOOP index="i" from="1" to="15000">
<CFSET ignored = barcomp.render_date_short(d = the_now)>
</CFLOOP>
<!--- foo4.cfm: test component-less function --->
<CFFUNCTION name="render_date_short" returntype="string" output="false">
<CFARGUMENT name="d" type="date" required="true">
<CFRETURN DateFormat(d, 'yyyy-mm-dd')>
</CFFUNCTION>
<CFSET the_now = Now()>
<CFLOOP index="i" from="1" to="15000">
<CFSET ignored = render_date_short(the_now)>
</CFLOOP>
The relative timings are:
foo1.cfm 32-33 sec
foo2.cfm 32-33 sec
foo3.cfm 16 sec
foo4.cfm 1 sec
This certainly makes me wonder about the wisdom of centralizing small utility
functions! Can anybody suggest a solution that improves on the speed of
component methods but still allows one to centralize common code?
cjeris Guest
-
Flex Functions or methods available in javascript
How do i know which methods and functions of flex are available in javascript? Are there some API's or documentation where we can get these... -
How to Extend the properties and methods of a MX 2004 Component (eg - ScrollPane)?
Does anyone have an example of how to add properties and extend functions of the version 2.0 Components that come with Flash MX 2004? ... -
Movieclip actions/functions/methods/...?
I'm having big trouble with my movie clips, as no actions seem to work. I have the movie clips exported to actionscript, and I'm using these names,... -
[PHP] functions/methods and what they should return
You want to break off things into as many functions and components as possible. The idea is that if you want to change the way tables are... -
functions/methods and what they should return
Hi people. I'm working on a large application right now (complete ecom store) and I'd like to get some feedback on something. Each product... -
cjeris #2
Re: Speed of component methods vs functions
One solution seems to be to copy the functions into the Request scope:
<!--- bar2.cfm : copies the component method into Request --->
<CFCOMPONENT>
<CFFUNCTION name="render_date_short" returntype="string" output="false">
<CFARGUMENT name="d" type="date" required="true">
<CFRETURN DateFormat(d, 'yyyy-mm-dd')>
</CFFUNCTION>
<CFSET Request.render_date_short = render_date_short>
</CFCOMPONENT>
<!--- foo5.cfm : calls the function from Request scope --->
<CFSET the_now = Now()>
<CFOBJECT name="barcomp" component="bar2">
<CFLOOP index="i" from="1" to="15000">
<CFSET ignored = Request.render_date_short(the_now)>
</CFLOOP>
foo5.cfm runs in 1 sec. So, we just have to copy all our utility functions
into Request scope so that they run without being treated as component methods.
Is there any reason why this would be wrong, assuming all the functions are
simple functions that don't attempt to access any component state?
cjeris Guest



Reply With Quote

