예제 #1
0
        public static void TestRun()
        {
            Console.WriteLine(GCompare <int> .CompareGeneric(3, 5));
            Console.WriteLine(GCompare <string> .CompareGeneric("bbb", "aaa"));
            //计算泛型集合的性能
            Stopwatch sw = new Stopwatch();
            //非泛型集合(数组)
            ArrayList al = new ArrayList();
            //泛型集合(数组)
            List <double> gl = new List <double>();

            //开始计时
            sw.Start();
            for (var i = 0; i < 10000000; i++)
            {
                //al.Add(i * 0.9);  //非泛型
                gl.Add(i * 0.9);  //泛型
            }
            // 结束计时
            sw.Stop();
            // 输出所用的时间
            TimeSpan ts          = sw.Elapsed;
            string   elapsedTime = string.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);

            Console.WriteLine("运行的时间: " + elapsedTime);
            Console.ReadKey();
        }
예제 #2
0
        static void BubbleS <T>(T[] arr, GCompare <T> compare)
        {
            int i = 0;
            int j = 0;
            T   temp;

            for (i = 0; i < arr.Length; i++)
            {
                for (j = (i >= arr.Length ? i + 1 : i); j < arr.Length; j++)
                {
                    if (compare(arr[i], arr[j]) > 0)
                    {
                        temp   = arr[i];
                        arr[i] = arr[j];
                        arr[j] = temp;
                    }
                }
            }
        }