Getting the tick count (GetTickCount API replacement)

System.Environment.TickCount
Use System.Environment.TickCount to get the number of milliseconds expired since session startup. Although this number is expressed in ms its actual resolution may be lower. A test on a I7 2.8GHZ CPU / Windows 7 64 Bit shows it’s interval is about 15 ms, also this value will wrap around, something to take in consideration.

DateTime.Now.Ticks
You can also use the DateTime.Now.Ticks which reports the number of ‘ticks’ in a 100ns resolution since 12:00:00 midnight, January 1, 0001. Being a 64 bit value it will not wrap around and is more accurate. Again this is not its true resolution. A test on a I7 2.8GHZ CPU / Windows 7 64 Bit shows it’s interval is 10000 x 100ns = 1 ms.

Exact timing, the StopWatch class
For exact timing purposes you can better use the System.Diagnostics.Stopwatch class. It uses the high resolution timers which are far more better.

Note: On any CPU you are never alone, especially on a single core the threads and processes share the time available. If you want to perform detailed timing on a multi-core CPU  you can use the CPU affinity mask trying to reserve a CPU core for your process alone (although you are never really sure the OS doesn’t share).

Lastest update in May 2011, inital post in May 2011

Write a comment

I appreciate comments, suggestions, compliments etc. Unfortunately I have no time to reply to them. Useful input will be used for sure!