您的位置:首页 > 编程语言

精确计算代码执行时间

2008-09-10 09:24 519 查看
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace cxb.RunTime
{
    /// <summary>
    /// 精确计算代码执行时间
    /// </summary>
    class RunTimeComputer
    {
        private static long start = 0;
        private static long end = 0;
        private static long freq = 0;
        private static double result = 0;

        [System.Runtime.InteropServices.DllImport("Kernel32.dll")]
        static extern bool QueryPerformanceCounter(ref long count);

        [System.Runtime.InteropServices.DllImport("Kernel32.dll")]
        static extern bool QueryPerformanceFrequency(ref long count);
        /// <summary>
        /// 测试模块开始
        /// </summary>
        public static void Start()
        {
            start = 0;
            freq = 0;
            QueryPerformanceFrequency(ref freq);
            QueryPerformanceCounter(ref start);
        }
        /// <summary>
        /// 测试模块结束
        /// </summary>
        public static void End()
        {
            end = 0;
            QueryPerformanceCounter(ref end);
        }
        /// <summary>
        /// 获得执行结果,返回运行时间
        /// </summary>
        /// <returns></returns>
        public static double GetRunTime()
        {
            start = end - start;
            result = (double)(start) / (double)freq;
            return result;
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  测试 class