C# 泛型集合

比较泛型集合和非泛型集合在运行中的效率:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ListDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("开始测试ArrayList:");
            TestBegin();
            TestArrayList();
            TestEnd();
            Console.WriteLine("开始测试List<T>:");
            TestBegin();
            TestGenericList();
            TestEnd();

            Console.Read();
        }

        static int collectionCount = 0;
        static Stopwatch watch = null;
        private static int testCount = 10000000;

        static void TestBegin()
        {
            GC.Collect();//强制对所有代码进行即时垃圾回收
            GC.WaitForPendingFinalizers();//挂起线程,执行终结器队列中的终结器(析构方法)
            GC.Collect();//再次对所有代码进行垃圾回收,主要包括从终结器队列中出来的对象
            collectionCount = GC.CollectionCount(0);//返回在0代中执行的垃圾回收次数
            watch = new Stopwatch();
            watch.Start();
        }

        static void TestEnd()
        {
            watch.Stop();
            Console.WriteLine("耗时:" + watch.ElapsedMilliseconds);
            Console.WriteLine("垃圾回收次数:"+(GC.CollectionCount(0)-collectionCount));
        }

        static void TestArrayList()
        {
            ArrayList al = new ArrayList();
            int temp = 0;
            for (int i = 0; i < testCount; i++)
            {
                al.Add(i);
                temp = (int) al[i];
            }
            al = null;
        }


        static void TestGenericList()
        {
            List<int> listT = new List<int>();
            int temp = 0;
            for (int i = 0; i < testCount; i++)
            {
                listT.Add(i);
                temp = listT[i];
            }
            listT = null;
        }
    }
}

C# 泛型集合

如果集合的数目固定并且不涉及转型,使用数组效率高否则就使用List<T>.

 

C# 泛型集合

上一篇:centOS下恢复win8引导


下一篇:C#Winform版之CheckBox、ChecklistBox控件遍历代码