Using the cftimer tag to time blocks of code



The cftimer tag displays execution time for a specified section of CFML code.

Using timing

Use this tag to determine how long it takes for a block of code to execute. This is useful when ColdFusion debugging output indicates excessive execution time, but does not pinpoint the long-running block of code.

To use this tag, enable debugging in the ColdFusion Administrator Debugging Settings page. In the Debugging Settings page, you must also specifically enable usage of the cftimer tag by checking the Timer Information check box.

If you enable debugging for the cftimer tag only and display timing information in an HTML comment, you can generate timing information without disturbing production users.

Calling the cftimer tag

You can control where the cftimer tag displays timing information, as follows:

  • Inline: Displays timing information following the </cftimer> tag.

  • Outline: Displays timing information at the beginning of the timed code and draws a box around the timed code. (This requires browser support for the HTML FIELDSET attribute.)

  • Comment: Displays timing information in an HTML comment in the format <!‑‑label: elapsed-timems >‑‑. The default label is cftimer.

  • Debug: Displays timing information in the debugging output under the heading CFTimer Times.

The following example calls the cftimer tag multiple times, each time using a different type attribute:

<HTML> 
<body> 
<h1>CFTIMER test</h1> 
<!--- type="inline" ---> 
    <cftimer label="Query and Loop Time Inline" type="inline"> 
        <cfquery name="empquery" datasource="cfdocexamples"> 
                select * 
                from Employees 
        </cfquery> 
 
        <cfloop query="empquery"> 
            <cfoutput>#lastname#, #firstname#</cfoutput><br> 
        </cfloop> 
    </cftimer> 
<hr><br> 
<!--- type="outline" ---> 
    <cftimer label="Query and CFOUTPUT Time with Outline" type="outline"> 
        <cfquery name="coursequery" datasource="cfdocexamples"> 
                select * 
                from CourseList 
        </cfquery> 
 
    <table border="1" width="100%"> 
        <cfoutput query="coursequery"> 
        <tr> 
        <td>#Course_ID#</td> 
        <td>#CorName#</td> 
        <td>#CorLevel#</td> 
        </tr> 
        </cfoutput> 
        </table> 
    </cftimer> 
<hr><br> 
<!--- type="comment" ---> 
    <cftimer label="Query and CFOUTPUT Time in Comment" type="comment"> 
        <cfquery name="parkquery" datasource="cfdocexamples"> 
                select * 
                from Parks 
        </cfquery> 
<p>Select View &gt; Source to see timing information</p> 
    <table border="1" width="100%"> 
        <cfoutput query="parkquery"> 
        <tr> 
        <td>#Parkname#</td> 
        </tr> 
        </cfoutput> 
        </table> 
    </cftimer> 
 
<hr><br> 
<!--- type="debug" ---> 
    <cftimer label="Query and CFOUTPUT Time in Debug Output" type="debug"> 
        <cfquery name="deptquery" datasource="cfdocexamples"> 
                select * 
                from Departments 
        </cfquery> 
<p>Scroll down to CFTimer Times heading to see timing information</p> 
    <table border="1" width="100%"> 
        <cfoutput query="deptquery"> 
        <tr> 
        <td>#Dept_ID#</td> 
        <td>#Dept_Name#</td> 
        </tr> 
        </cfoutput> 
        </table> 
    </cftimer> 
</body>