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

本文介绍了一个用于测量算法运行时间的C#实现方案。通过调用内核API获取高精度计时器读数来准确记录算法开始和结束的时间,并计算总的执行时间。文中还展示了一个插入排序算法的实例,演示了如何使用该计时器类来衡量排序操作的耗时。
摘要由CSDN通过智能技术生成
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;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值