Speed of component methods vs functions

Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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? ...
    3. 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,...
    4. [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...
    5. 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...
  3. #2

    Default 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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139