comment
I use

DateDiff("s", CreateDate(1970,1,1), now())

comment
Using java.util.Date is almost exactly 50% faster than the DateDiff() in CFMX8.

<cfset jDate = createObject('java', 'java.util.Date').getTime()>
<cfloop index='x' to='100000' from='1'>
<cfset tmpp = createObject('java', 'java.util.Date')>
</cfloop>
<cfoutput><br>#(createObject('java', 'java.util.Date').getTime() - jDate)#ms</cfoutput>


produced an average of 694ms on 10 runs.

<cfset jDate = createObject('java', 'java.util.Date').getTime()>
<cfloop index='x' to='100000' from='1'>
<cfset tmpp = DateDiff('s', CreateDate(1970,1,1), now())>
</cfloop>
<cfoutput><br>#(createObject('java', 'java.util.Date').getTime() - jDate)#ms</cfoutput>


produced an average of 1440ms on 10 runs.

comment
04/16/2009 05:39PM | Stewart Gateley
This is exactly what the ColdFusion built-in function GetTickCount() is for.

Also, you are way off on your measurements of how fast each method is. You can't measure the whole page load time as there are many factors there to skew your results.

I ran several tests using GetTickCount(), CreateObject('java','java.util.Date').getTime() and DateDiff('s', CreateDate(1970,1,1), Now()) with a timer wrapped around each of them, in almost all cases every method executed in sub 1 ms and never above 1 ms for any call. The times that I did see 1ms was most likely my internal counter passing to the next ms during execution, thus I believe all calls are just as efficient, however GetTickCount() is much shorter and easier to use.

Test code:


#GetTickCount()# - #GetTickCount() - now# ms


#CreateObject('java','java.util.Date').getTime()# - #GetTickCount() - now# ms


#DateDiff('m', CreateDate(1970,1,1), Now())# - #GetTickCount() - now# ms

Note: The last method only returns the number of seconds, not the number of milliseconds like the prior two.

comment
04/16/2009 05:53PM | Stewart Gateley
I just did another 10 tests using cftimer and turned on debugging. GetTickCount() and CreateObject(...) both produced 0 ms on all 10 runs. DateDiff(...) hit 1 ms on 2 runs. I would say any method is safe to use, but the later two are both pointless.