High performance timing in C #

zhaozj2021-02-12  145

High performance timing in C #

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 ITTER 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 resolution performance counter. For more information (including What happensiff means high resolution performance counter) please check msdn for queryperformancecounter and queryperFormanceFrequency.

PUBLIC CLASS HIGHRESOLES OF THE (PRIVATE "

Public highResolutionTimer () {queryperformancefrequency (reffrequency);}

Public void start () {queryperformancecounter (ref start);}

Public void stop () {queryperformancecounter (ref stop);}

Public float elapsedtime {get {float elapsed = ((Float) / (FLOAT) / (Float)); Return Elapsed;}}

[System.Runtime.InteropServices.DllImport ( "KERNEL32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)] private static extern bool QueryPerformanceCounter ([In, Out] ref long performanceCount); [System.Runtime.InteropServices .DllImport ( "KERNEL32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)] private static extern bool QueryPerformanceFrequency ([In, Out] ref long frequency);} To illustrate the use of this class, check the code Below.

Highresolutiontimer Timer = new highresolutiontimer (); timer.Start (); // perf test timer.stop (); console.writeline (Timer.ELAPSedTime);

(This Posting is provided "As IS" with no warranties, and confrs no rights. Use of include to the terms specified at http://www.microsoft.com/info/c relayright.htm)

转载请注明原文地址:https://www.9cbs.com/read-6626.html

New Post(0)