DateTime.Now's accuracy is very low, this low means that the difference between the Ticks of DateTime.Now two acquisition is just a large number of integer times. For example, on my machine, this difference is 10.114ms. So, if I use datetime.now to calculate the time difference, then it cannot be accurate to 10 ms.
Later, it was found that the accuracy of ASP.NET's Trace is very high, and it is realized by reflector, discovers it to use these two methods:
Reference MSDN: How to: Time Managed Code Using QueryperFormanceCounter and QueryperFormanceFrequency
I have written a class according to this, and the code is as follows.
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;} else {_f = -1;}}} if (f == -1) {return environment.tickcount * 10000;} long c = 0; queryperformancecounter (REF C); return (((Double) c) * 1000 * 1000 / ((double) f));} // gettickcount () is 0 times the datetime.ticks value static long _tc = 0; // This return is not true accurate time, but time and time The difference is precise. // getexactnow is small than the accuracy of DateTime.Now is small, so the deviation static public datetime getExactNow () {if (_tc == 0) {long tc = gettickcount (); datetime dt = datetime.now ; _TC = DT.TICKS - TC; Return Dt;} Return New DateTime (_tc gettickcount ());}}
In ASP. NET's application, you can join the code to the Tickcount:
Context.Items ["BeginRequestTickCount"] = a.gettickcount ();
Then on the page output:
....
This will reach the acquisition page running time value. (Of course, the ASP.NET has some later work after outputting Totalmilliseconds, but this time should only need 0.N ms) DateTime.now's accuracy is very low, this low means, two Ticks acquired Datetime.now Ticks The difference is just a large number of integer times. For example, on my machine, this difference is 10.114ms. So, if I use datetime.now to calculate the time difference, then it cannot be accurate to 10 ms. Later, it was found that the accuracy of ASP.NET's Trace is very high, and it is realized by reflector, discovers it to use these two methods:
Reference MSDN: How to: Time Managed Code Using QueryperFormanceCounter and QueryperFormanceFrequency
I have written a class according to this, and the code is as follows.
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;} else {_f = -1;}}} if (f == -1) {return environment.tickcount * 10000;} long c = 0; queryperformancecounter (REF C); return (((Double) c) * 1000 * 1000 / ((double) f));} // gettickcount () is 0 times the datetime.ticks value static long _tc = 0; // This return is not true accurate time, but time and time The difference is precise. // getexactnow is small than the accuracy of DateTime.Now is small, so the deviation static public datetime getExactNow () {if (_tc == 0) {long tc = gettickcount (); datetime dt = datetime.now ; _TC = DT.TICKS - TC; Return Dt;} Return New DateTime (_tc gettickcount ());}}
In ASP. NET's application, you can join the code to the Tickcount:
Context.Items ["BeginRequestTickCount"] = a.gettickcount ();
Then on the page output:
....