在C#中实现高性能计时

本文介绍了在C#中进行性能测试时如何测量代码执行时间的重要性。System.Environment.TickCount不适合高分辨率计时,而System.DateTime.Now虽然比它更好但仍然不是高分辨率的。在.NET框架1.1及更早版本,需要使用P/Invoke调用QueryPerformanceCounter和QueryPerformanceFrequency获取高分辨率的性能计数器。提供了一个名为HighResolutionTimer的类,用于查询硬件获取高精度时间。
摘要由CSDN通过智能技术生成

For performance test, it is very important to measure code execution time. Without measurement, there is no way to tell if we meet performance goal.

System.Environment.TickCount is not suitable for high resolution timing. Its resolution cannot be less than 500 milliseconds.

System.Datetime.Now  returns the current time of type DateTime. With start datetime and end datetime, we can get the interval as a value of TimeSpan by (end - start ) . TimeSpan.TotalMilliseconds or TimeSpan.Ticks may be used to read interval. From MSDN, the resolution of System.Datetime.Now  depends on the system timer.

System Approximate Resolution
Windows NT 3.5 and later 10 milliseconds
Windows 98 55 milliseconds

So it is better but not high resolution at all.


In .NET framework v1 and v1.1, we have to use P/Invoke to get high resolution reading. The class below is commonly used in performance test measurement. It is querying hardware to get high r

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值