Created
October 4, 2015 06:55
-
-
Save DamianEdwards/e282c09f32b727e268a2 to your computer and use it in GitHub Desktop.
Quick test of system timer frequency vs. Environment.TickCount in .NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace ConsoleApp3 | |
{ | |
public class Program | |
{ | |
public void Main(string[] args) | |
{ | |
var length = TimeSpan.FromSeconds(2); | |
var started = DateTime.UtcNow; | |
var lastTickCount = Environment.TickCount; | |
var lastNow = DateTime.UtcNow; | |
while (DateTime.UtcNow - started < length) | |
{ | |
var tickCount = Environment.TickCount; | |
var now = DateTime.UtcNow; | |
if (lastTickCount != tickCount) | |
{ | |
Console.WriteLine($"Environment.TickCount changed: +{tickCount - lastTickCount}ms"); | |
} | |
var diff = now - lastNow; | |
if (diff.Ticks > 0) | |
{ | |
Console.WriteLine($"DateTime.UtcNow changed: +{diff.TotalMilliseconds}ms"); | |
} | |
lastTickCount = tickCount; | |
lastNow = now; | |
} | |
Console.WriteLine(); | |
Console.WriteLine($"INFO: {TimeSpan.TicksPerMillisecond} TimeSpan ticks / ms"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment