线程等待时间(不准确)
1.封装系统API函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Speed
{
public class TimePeriod
{
[DllImport("winmm.dll", EntryPoint = "timeBeginPeriod")]
public static extern uint MM_BeginPeriod(uint uMilliseconds);
[DllImport("winmm.dll", EntryPoint = "timeEndPeriod")]
public static extern uint MM_EndPeriod(uint uMilliseconds);
[DllImport("winmm.dll")]
public static extern uint timeGetTime();
}
}
2.设置环境
TimePeriod.MM_BeginPeriod(1);
Thread.Sleep(4);
TimePeriod.MM_EndPeriod(1);
高精度计时
TimePeriod.MM_BeginPeriod(1);
uint startT = TimePeriod.timeGetTime();
uint endT = TimePeriod.timeGetTime();
uint tMs = endT - startT;
TimePeriod.MM_EndPeriod(1);