Timer Example: How to time your code
Introduction
We all know that timing code is needed for debugging and optimization purposes. ColdFusion already implements this with the CFTIMER tag. I use this a lot. However, most of the time I do not have the coldfusion debugging turned on, but I do have my ColdBox debugging panel on. So what do I do to time code execution. Well, the timer plugin.
- Please look at the Timer Plugin API
Usage
The timer plugin can be used in three ways:
logTime() Method
Using the logTime() method, this requires for you to time the execution and pass the results to the timer.
<!--- Start timing using the getTickCOunt method() ---> <cfset stime = getTickcount()> <!--- All the code I want to time below ---> ... ... <!---Log Total Time with plugin ---> <cfset getPlugin("timer").logTime("My Label",totaltime)>
start() stop() Method
As a true timer using the start() and stop() methods. This is the preferred way of calling the timer.
<!--- Start Timer ---> <cfset getPlugin("timer").start("MyLabel")> <!--- All the code I want to time below ---> ... ... <!--- Stop & Log timer ---> <cfset getPlugin("timer").stop("MyLabel")>
Custom Tag Method
The third and final way is to use the ColdBox timer custom tag and it is used similar to the cftimer tag:
<!--- Call the timer tag ---> <cfmodule template="/coldbox/system/includes/timer.cfm" timertag="Invoking Some Code Label"> <!--- All the code I want to time ---> ... ... </cfmodule>
Conclusion
And there you go. You can now time code executions with ease using either the timer plugin or the timer tag included with ColdBox. Helping developers code with ease.
