C#性能测试模板和洗牌的随机排序算法

2021-11-03 10:11:41 浏览数 (1)

代码语言:javascript复制
using System;
namespace SpeedTestTemplate
{
    class Program
    {
        static void Main()
        {
            //确定运行次数
            const int RUN_NUMBER = 1000000;

        //声明开始和结束时间变量
        long startTime , endTime;

        //开始测试
        startTime = DateTime.Now.Ticks;
        for (int i = 1; i <= RUN_NUMBER; i  )
        {
            //需要的代码
        }
        endTime = DateTime.Now.Ticks;

        //显示运行所用时间
        Console.WriteLine("运行时间为:{0}", endTime - startTime);
    }
}
 
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ArrayDemo3
{
    class Program
    {
        static void Main(string[] args)
        {
            //定义扑克牌数组
            int[] cards = new int[54];
            //设置扑克牌值
            for (int i = 0; i < cards.Length; i  )
            {
                cards[i] = i   1;
            }
            //洗牌
            Random randomBuilder = new Random();
            int swapTarget , swapTemp;
            for (int i = 0; i < cards.Length; i  )
            {
                //随机指定交换目标索引
                swapTarget = randomBuilder.Next(0, cards.Length);
                //交换
                swapTemp = cards[i];
                cards[i] = cards[swapTarget];
                cards[swapTarget] = swapTemp;
            }
            //使用数组值
            Console.WriteLine("第5张牌的值是{0}", cards[4]);
        }
    }
}
</pre> 

0 人点赞