GetTickCount() Precision Argument In Lucee CFML 5.3.7.47
The other day, while looking through some Lucee CFML documentation, I came across an example that used the built-in getTickCount()
function with an argument. I've been using getTickCount()
since the beginning of time and I never once noticed that it accepted an argument (at least in the Lucee CFML runtime). As such, I thought it might be worth sharing this feature more broadly.
By default, the getTickCount()
function returns the milliseconds since Epoch (aka, "System Time"). This function is incredibly useful when it comes to measuring the performance of a block of code (by measuring the delta between a pre-and-post call to getTickCount()
). In Adobe ColdFusion - from what I can see - this is the only invocation format. However, in Lucee CFML, you can pass-in the following arguments:
millisecond
- equivalent to default invocation.milli
- equivalent to default invocation.second
micro
nano
To see this in action, I've set up a quick demo:
<cfscript>
// Milliseconds since Epoch - the default.
echoLine( "Default", getTickCount() );
// Alternate formats.
echoLine( "Millisecond", getTickCount( "millisecond" ) );
echoLine( "Milli", getTickCount( "milli" ) );
echoLine( "Second", getTickCount( "second" ) );
echoLine( "Micro", getTickCount( "micro" ) );
echoLine( "Nano", getTickCount( "nano" ) );
// ------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------- //
public void function echoLine(
required string label,
required string value
) {
echo( "<dt><strong>#label#:</strong></dt><dd>#value#</dd>" );
}
</cfscript>
And, when we run this Lucee CFML code, we get the following output:
As you can see, the argument "second"
gives us lower precision; and, the arguments "micro"
and "nano"
give us higher precision time values.
I don't have an immediate need for these levels of granularity - the default millisecond result for getTickCount()
works well for my use-cases. But, I suppose this would be helpful for micro-benchmarks where you're trying to super-tune some block of code? Anyway, I didn't know this existed; so, I figured there are others who might be curious to know.
Want to use code from this post? Check out the license.
Reader Comments
Sadly, ACF GetTickCount() does NOT support these arguments. I love all the thoughtful adds Lucee gives us. I'm currently working on a long-running process which creates JSON packets for every user (over 30k users) and sends them to another service to sync the two. It takes about 700ms per transaction. I keep track of the processingTime per transaction as well as for each collection/batch. For the batch I have to use ms/1000 math to get seconds...not a big deal, but would be nice to pass "seconds" in as a parameter instead!
@Chris,
I do like the little Lucee details. Also, over on Twitter, Zack Spitzer was saying that they are also adding this granularity to the
<CFTimer>
tag as well; along with the ability to get the time as a variable. I think he said it would be something like this:It would be an interesting way to use the timer tag.
@Ben
I had totally forgotten (blocked? wiped?) the
cftimer
tag from my memory. I only ever useGetTickCount()
and do the math to calculate duration. That would be a super clever use of the tag though!