QueryPerformanceCounter() and Stopwatch.GetTimestamp() both return the tick count from one of the processors. I’d like to use the returned 64-bit integer value as a unique ID and I wonder if it’s possible that these functions return the same value when they’re called multiple times in a process.
My process would run for few hours at most at a time and these unique IDs are only used temporarily – they’re not saved in a database, for example. The above functions would be called several times but with other function calls in between.
The documentation states that it’s possible that a motherboard doesn’t support high-performance timers but all I’ve worked with had this support. My code would only run on 32-bit or 64-bit server hardware where I believe high-performance timers are always supported anyway (I’m not sure, though).
I could write a small test program to test it but that test would only be valid on my own computer so I’m looking for a more definitive answer, if one is available.
Edit: I know these functions are not designed to generate unique IDs but as long as they generate ever-increasing values, it’s fine. I’m asking this question not only for technical reason but out of curiosity as well.
Neither of the functions designed to return unique IDs. In fact it is likely 2 subsequent calls will return the same value.
For unique identifiers consider using Guid.NewGuid or simply have counter with (interlocked) increment on each “give me ID” call.
Proof of concept for “QueryPerformanceCounter” returns the same value: