Does getTickCount() Generate Unique Results?

ColdFusion Add comments

As we know whenever we need to have some unique IDs in our applications, we are using "CreateUUID()" function to generate unique results.

I have plan to use getTickCount() function to generate IDs in my applications but I really would like to be sure whether it generates unique results or not. If we should have unique results I think it would be an alternative to the UUID especially when we compare as table keys in integer format.

When I check the ColdFusion 8 documentation page "Creating a CAPTCHA image", it is mentioned as "Use the GetTickCount function to generate unique names for the CAPTCHA files." and I understand from here that it generates unique results.

What would be your idea? :)

4 responses to “Does getTickCount() Generate Unique Results?”

  1. Zac Spitzer Says:
    mmm, sounds wrong to me... two threads on a multi-core system could call getTickCount() at the same time.

    why not use an application or server counter variable with proper locking?
  2. Jason Morgan Says:
    Hi Oğuz -

    I would not recommend this. getTickCount uses the system's ms timer to get the tick count which is not enough resolution. Even if it was in nanosecond time, this does not seem like a good idea.

    This code returns the same tick count every time for me when ran locally :

    <cfthread name="t1" action="run">
       <cfset thread.tick = getTickCount() />
    </cfthread>
    <cfthread name="t2" action="run">
       <cfset thread.tick = getTickCount() />
    </cfthread>
    <cfthread name="t3" action="run">
       <cfset thread.tick = getTickCount() />
    </cfthread>

    <cfthread action="join" name="t1,t2,t3" timeout="6000" />

    <cfdump var="#cfthread#">
  3. Rob Brooks-Bilson Says:
    GetTickCount() returns epoch, so if you request two calls to the func at the same time, you'll get the same number. It's not granular enough.

    Why don't you want to use createUUID()? Is it because it's too slow? If so, drop down to java and generate the uuids there. Or, id you need the ids for primary keys in your DB, what's wrong with an auto increment/identity key? Or, let your db generate a GUID if it's sql server.
  4. Oğuz Demirkapı Says:
    @Jason: I see your point.

    @Rob: It was just an idea and would be valuable when you compare for efficiency between generated UUIDs and numeric results from getTickCount. I am not a big supporter to use an alpha numeric value in primary keys. Having own keys is also an old habit because of data transfer problem on different DB solutions. If it is possible I prefer having my own keys instead of auto increment keys. GUID idea is also a nice alternative for SQL Server. I will try to find similar thing on MySQL.

    Thanks for your input.

Leave a Reply





Powered by Mango Blog. Design and Icons by N.Design Studio