Created
May 16, 2019 02:26
-
-
Save KumoKyaku/1dcbe87ad31f22e769466c79f081fd31 to your computer and use it in GitHub Desktop.
测试系统时间精度
This file contains hidden or 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; | |
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