您的位置:首页 > 其它

测试算法执行时间程序片段

2012-06-05 17:48 246 查看
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Threading;

namespace IntroductionToAlgorithms
{
    class RunTime
    {
        [DllImport("Kernel32.dll")]
        private static extern bool QueryPerformanceCounter(
            out long lpPerformanceCount);

        [DllImport("Kernel32.dll")]
        private static extern bool QueryPerformanceFrequency(
            out long lpFrequency);

        private long startTime, stopTime;
        private long freq;

        // Constructor
        public RunTime()
        {
            startTime = 0;
            stopTime = 0;

            if (QueryPerformanceFrequency(out freq) == false)
            {
                // high-performance counter not supported
                throw new Win32Exception();
            }
        }

        // Start the timer
        public void Start()
        {
            // lets do the waiting threads there work
            Thread.Sleep(0);

            QueryPerformanceCounter(out startTime);
        }

        // Stop the timer
        public void Stop()
        {
            QueryPerformanceCounter(out stopTime);
        }

        // Returns the duration of the timer (in seconds)
        public double Duration
        {
            get
            {
                return (double)(stopTime - startTime) / (double)freq;
            }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Text;

namespace IntroductionToAlgorithms
{
    class Program
    {
        static void Main(string[] args)
        {
            RunTime rt = new RunTime();
            int[] arr = CreateArr(10*10000);
            int count = -1;

            int showSize = 100;

            if (arr.Length <= showSize) Program.Show("原数组:" , arr);

            rt.Start();

            count = S2_1_Insertion_Sort.Asc(arr);

            rt.Stop();

            if (arr.Length <= showSize) Program.Show("排序后:" , arr);
            Console.WriteLine("循环次数:"+count.ToString());
            Console.WriteLine("排序耗时:" + rt.Duration.ToString());

            Console.ReadLine();
        }

        public static void Show(string mark,int[] arr)
        {
            Console.WriteLine(mark);
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i].ToString()+"  ");
            }
            Console.WriteLine();
        }

        public static int[] CreateArr(int length)
        {
            RunTime rt = new RunTime();
            rt.Start();
            int[] arr=new int [length];
            for (int i = 0; i < length; i++)
            {
                arr[i] = i;
            }

            for (int i = arr.Length-1; i >=0; i--)
            {
                Random random = new Random();
                int index=random.Next(i);

                int temp = arr[i];
                arr[i] = arr[index];
                arr[index] = temp;
            }
            rt.Stop();
            Console.WriteLine("创建连续随机数组,数组大小:"+length+"  耗时:"+rt.Duration.ToString());
            return arr;
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: