Comparison of Environment.TickCount, DateTime.Now.Ticks and Stopwatch.ElapsedMilliseconds

By | 2015.08.17

Doing game development I use a lot of timers. Every Update() I check if its time to do something. To make this as light as possible I made a small benchmark to check the speed of different ways of getting time.

When running the code ignore the first 1000-result from each test (used for warmup). Also ignore the number printed after “ignore:”, its just there just to ensure no compiler optimization is done that would affect the result.

Results on my computer:

Both DateTime.Now.Ticks (test 2) and Stopwatch.ElapsedMilliseconds (test 3) are considerably slower than Environment.TickCount (test 1), but not enough to be noticeable unless you are performing a lot of calculations. For example I was investigating this because I need a cheap way of getting time in tight game loops. The thing to note is that Environment.TickCount is 32-bit signed which means that after 2147483 seconds of uptime (24,85513 days) the counter overflows and goes to –2147483648 (minus 24,85513 days).

Tip! You can force an overflow to 0 after a max value of 49 days by converting it to an unsigned integer: var value = unchecked((UInt32)Environment.TickCount)

Benchmark code:


Leave a Reply