商城首页欢迎来到中国正版软件门户

您的位置:首页 > 硬件相关 >C# 六种精确计时方法详解

C# 六种精确计时方法详解

  发布于2025-05-17 阅读(0)

扫一扫,手机访问

C# 六种方式实现精确计时

根据综合网上的多篇文章,实现精确计时的主要方法有以下几种:

  1. 调用WIN API中的GetTickCount函数:
[DllImport("kernel32")]static extern uint GetTickCount();

此函数返回自操作系统启动以来经过的毫秒数,精度大约为1毫秒,但测试显示误差约为15毫秒。缺点是返回值为uint类型,最大值为2的32次方,因此在服务器连续运行约49天后,返回值会归零。

用法:

uint s1 = GetTickCount();Thread.Sleep(2719);Console.WriteLine(GetTickCount() - s1);  //单位毫秒
  1. 调用WIN API中的timeGetTime函数,推荐使用:
[DllImport("winmm")]static extern uint timeGetTime();

此函数常用于多媒体定时器,返回自操作系统启动以来经过的毫秒数,精度为1毫秒。默认精度可能超过1毫秒,需使用timeBeginPeriod和timeEndPeriod函数来设置精度。

[DllImport("winmm")]static extern void timeBeginPeriod(int t);[DllImport("winmm")]static extern void timeEndPeriod(int t);

缺点与GetTickCount类似,受返回值最大位数限制。

用法:

timeBeginPeriod(1);uint start = timeGetTime();Thread.Sleep(2719);Console.WriteLine(timeGetTime() - start);  //单位毫秒timeEndPeriod(1);
  1. 使用.NET自带的System.Environment.TickCount方法:

此方法返回自系统启动以来经过的毫秒数。经反编译猜测它可能调用了GetTickCount,但返回值类型为int。缺点与GetTickCount相同,受返回值最大位数限制。

用法:

int aa = System.Environment.TickCount;Thread.Sleep(2719);Console.WriteLine(System.Environment.TickCount - aa); //单位毫秒

注:测试发现,GetTickCount和System.Environment.TickCount也可以通过timeBeginPeriod和timeEndPeriod来设置精度,最高可达1毫秒。

  1. 调用WIN API中的QueryPerformanceCounter函数:
[DllImport("kernel32.dll ")]static extern bool QueryPerformanceCounter(ref   long lpPerformanceCount);

此函数用于获取高精度计时器的值,需与QueryPerformanceFrequency函数配合使用,后者返回硬件支持的高精度计数器频率。

[DllImport("kernel32")]static extern bool QueryPerformanceFrequency(ref long PerformanceFrequency);

用法:

long a = 0;QueryPerformanceFrequency(ref a);long b = 0, c = 0;QueryPerformanceCounter(ref b);Thread.Sleep(2719);QueryPerformanceCounter(ref c);Console.WriteLine((c - b) / (decimal)a);  //单位秒

精度可达百万分之一秒,且使用long类型避免了位数不足的问题。缺点是其结果在节能模式下可能变慢,超频模式下可能变快,且在笔记本电脑上使用电池和接电源时效果不同。

  1. 使用.NET的System.Diagnostics.Stopwatch类,推荐使用:

Stopwatch类通过计时器刻度计数来测量运行时间。如果硬件和操作系统支持高分辨率性能计数器,则使用QueryPerformanceCounter;否则,使用DateTime.Ticks来计算。

用法:

Stopwatch sw = new Stopwatch();sw.Start();Thread.Sleep(2719);sw.Stop();Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency);
  1. 使用CPU时间戳进行更高精度的计时:

此方法的精度可达纳秒(ns),但需要在C#中通过托管C++项目(包含汇编代码)来实现,较为复杂。

C++代码:

// MLTimerDot.h #pragma once using namespace System; namespace MLTimerDot {         //得到计算机启动到现在的时钟周期        unsigned __int64 GetCycleCount(void)        {                _asm  _emit 0x0F                _asm  _emit 0x31        }          //声明 .NET 类        public __gc class MLTimer        {        public:                MLTimer(void)                {                                       }                 //计算时钟周期                UInt64 GetCount(void)                {                        return GetCycleCount();                }         };}

C#调用:

long a = 0;QueryPerformanceFrequency(ref a); MLTimerDot.MLTimer timer = new MLTimerDot.MLTimer();ulong ss= timer.GetCount();Thread.Sleep(2719);Console.WriteLine((timer.GetCount() - ss) / (decimal)a);

缺点与QueryPerformanceCounter类似,结果不太稳定。

我的结论:在常规应用中,timeGetTime函数足以满足需求,将精度设置为1毫秒即可。System.Diagnostics.Stopwatch类因其调用方便,也推荐使用。

热门关注