|
获取高精度的时间差,可以用来分析页面运行时间的长短 Author:lostinet From: joycode DateTime.Now的精度是很低,这个低的意思是,两次获取的DateTime.Now的Ticks的差,只是一个较大数的整数倍。例如在我的机器上,这个差最小是10.114ms。所以,如果我用DateTime.Now来计算时间差,那么就无法精确到10ms以内。 后来发现asp.net的trace的精度很高,用reflector看它的实现,发现了它是使用这两个方法的: 参考msdn:How To: Time Managed Code Using QueryPerformanceCounter and QueryPerformanceFrequency 我自己了按照这个写了个类,代码如下 using System; using System.Runtime.InteropServices; public class A { [DllImport("kernel32.dll")] static extern bool QueryPerformanceCounter([In, Out] ref long lpPerformanceCount); [DllImport("kernel32.dll")] static extern bool QueryPerformanceFrequency([In, Out] ref long lpFrequency); static long _f = 0; static public long GetTickCount() { long f = _f; if (f == 0) { if (QueryPerformanceFrequency(ref f)) { _f = f; } [1] [2] [3] 下一页
|