Skip to main content
Ben Nadel at Scotch On The Rock (SOTR) 2010 (London) with: Guust Nieuwenhuis and Cyril Hanquez
Ben Nadel at Scotch On The Rock (SOTR) 2010 (London) with: Guust Nieuwenhuis ( @Lagaffe ) Cyril Hanquez ( @Fitzchev )

GetTickCount() Precision Argument In Lucee CFML 5.3.7.47

By on
Tags:

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:

Various results of getTickCount() with an argument in Lucee CFML

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

208 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!

15,688 Comments

@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:

<cftimer variable="duration" units="nano">
	... processing things ...
</cftimer>

<cfoutput>Duration: #numberFormat( duration )# nanoseconds</cfoutput>

It would be an interesting way to use the timer tag.

208 Comments

@Ben

I had totally forgotten (blocked? wiped?) the cftimer tag from my memory. I only ever use GetTickCount() and do the math to calculate duration. That would be a super clever use of the tag though!

Post A Comment — I'd Love To Hear From You!

Post a Comment

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel