Skip to content

Instantly share code, notes, and snippets.

@KumoKyaku
Created May 16, 2019 02:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KumoKyaku/1dcbe87ad31f22e769466c79f081fd31 to your computer and use it in GitHub Desktop.
Save KumoKyaku/1dcbe87ad31f22e769466c79f081fd31 to your computer and use it in GitHub Desktop.
测试系统时间精度
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace TimePeriod
{
class Program
{
[DllImport("winmm.dll")]
internal static extern uint timeBeginPeriod(uint period);
[DllImport("winmm.dll")]
internal static extern uint timeEndPeriod(uint period);
const int sleeptime = 5;
static void Main(string[] args)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Restart();
Thread.Sleep(sleeptime);
stopwatch.Stop();
Console.WriteLine($"Thread.Sleep({sleeptime}) Normal--{stopwatch.ElapsedMilliseconds}ms--{stopwatch.ElapsedTicks}Ticks");
timeBeginPeriod(1);
stopwatch.Restart();
Thread.Sleep(sleeptime);
stopwatch.Stop();
Console.WriteLine($"Thread.Sleep({sleeptime}) timeBeginPeriod(1)--{stopwatch.ElapsedMilliseconds}ms--{stopwatch.ElapsedTicks}Ticks");
timeEndPeriod(1);
Task.Run(() =>
{
int count = 50;
while (count > 0)
{
count--;
stopwatch.Restart();
Thread.Sleep(sleeptime);
Console.WriteLine($"Thread.Sleep({sleeptime}) Normal--{stopwatch.ElapsedMilliseconds}ms--{stopwatch.ElapsedTicks}Ticks");
stopwatch.Stop();
}
});
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment