当前位置:首页 > 网络编程 > 软件语言 > .NET > 在C#中实现高性能计时

在C#中实现高性能计时

点击次数:54 次 发布日期:2008-11-06 08:06:02 作者:源代码网
源代码网推荐
广告载入中
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 resolution performance counter. For more information (including what happens if the hardware does not support high resolution performance counter) please check MSDN for QueryPerformanceCounter and QueryPerformanceFrequency.
源代码网推荐
源代码网推荐public class HighResolutionTimer
源代码网推荐{
源代码网推荐 private long start;
源代码网推荐 private long stop;
源代码网推荐 private long frequency;
源代码网推荐
源代码网推荐 public HighResolutionTimer()
源代码网推荐 {
源代码网推荐 QueryPerformanceFrequency (ref frequency);
源代码网推荐 }
源代码网推荐
源代码网推荐 public void Start ()
源代码网推荐 {
源代码网推荐 QueryPerformanceCounter (ref start);
源代码网推荐 }
源代码网推荐
源代码网推荐 public void Stop ()
源代码网推荐 {
源代码网推荐 QueryPerformanceCounter (ref stop);
源代码网推荐 }
源代码网推荐
源代码网推荐 public float ElapsedTime
源代码网推荐 {
源代码网推荐 get{
源代码网推荐 float elapsed = (((float)(stop - start)) / ((float) frequency));
源代码网推荐 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 confers no rights. Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm)
源代码网推荐
源代码网推荐
源代码网推荐
源代码网推荐

源代码网推荐

源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华