当前位置:编程学习 > C#/ASP.NET >>

通用的用于测试程序运行时间的C#类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;

namespace 简易系统
{
    public class PerformanceTimer
    {
        public static readonly bool IsHighPerformance;

        [DllImport("Kernel32.dll")]
        private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
        //Retrieves the current value of the high-resolution performance counter.

        [DllImport("Kernel32.dll")]
        private static extern bool QueryPerformanceFrequency(out long lpFrequency);
       //Retrieves the frequency of the high-resolution performance counter, if one exists. 
        //The frequency cannot change while the system is running.

        private long m_startTime;
        private long m_stopTime;
        private static long m_freq;

        static PerformanceTimer()
        {
            try
            {
                IsHighPerformance = QueryPerformanceFrequency(out m_freq);
            }
            catch (Exception)
            {
                IsHighPerformance = false;
            }
        }

        public PerformanceTimer()
        {
            m_startTime = 0;
            m_stopTime = 0;
        }

        /// <summary>
        /// Start the timer
        /// </summary>
        public void Start()
        {
            // let the waiting threads do their work
            Thread.Sleep(0);

            if (IsHighPerformance)
            {
                QueryPerformanceCounter(out m_startTime);
            }
            else
            {
                m_startTime = DateTime.Now.Ticks;
            }
        }

        /// <summary>
        /// Stop the timer
        /// </summary>
        public void Stop()
        {
            if (IsHighPerformance)
            {
                QueryPerformanceCounter(out m_stopTime);
            }
            else
            {
                m_stopTime = DateTime.Now.Ticks;
            }
        }

        /// <summary>
        /// Returns the duration of the timer (in fraction of seconds)
        /// </summary>         
        public double DurationSeconds
        {
            get
            {
                if (IsHighPerformance)
                {
                    return (double)(m_stopTime - m_startTime) / (double)m_freq;
                }
                else
                {
                    TimeSpan span = (new DateTime(m_stopTime)) - (new DateTime(m_startTime));
                    return span.TotalSeconds;
                }
            }
        }
        public double DurationMilliseconds
        {
            get
            {
              if(IsHighPerformance)
              {
                  return (double)(m_stopTime - m_startTime) / (double)m_freq;
              }
                else
              {
                  TimeSpan span = (new DateTime(m_stopTime)) - (new DateTime(m_startTime));
                  return span.TotalMilliseconds;
              }
            }
        }
    }
}
//////测试用例1////////////
static void Main() {
PerformanceTimer timer = new PerformanceTimer();
timer.Start();
// do something here
timer.Stop();
Console.WriteLine(timer.DurationSeconds);
}
/////////////////////////////////////////////第二个类/////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace 简易系统
{
    class Timing
    
    {
        TimeSpan startingTime;
        TimeSpan duration;
        public Timing()
        {
            startingTime = new TimeSpan(0);
            duration = new TimeSpan(0);
        }
        public void StopTime()
        {
            duration = Process.GetCurrentProcess().Threads[0].UserProcessorTime.Subtract(startingTime);
        }
        public void startTime()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            startingTime = Process.GetCurrentProcess().Threads[0].UserProcessorTime;
        }
        public TimeSpan Result()
        {
            return duration;
        }
    }
    
}
////////////////////////////测试用例2////////
static main()
{
TimeSpan ts = Process.GetCurrentProcess().TotalProcessorTime;
           Stopwatch sw = new Stopwatch();
            sw.Start();          
            //do something here 这里是要运行的代码
            sw.Stop();
            double mis = Process.GetCurrentProcess().TotalProcessorTime.Subtract(ts).TotalMilliseconds;
            label3.Text = "CPU(ms):" + mis.ToString();
            label5.Text = "process cost:"+Convert.ToString(sw.Elapsed.TotalMilliseconds);
} --------------------编程问答-------------------- 两个类都可以使用,第一个更方便
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,