C#内List VS hashset性能对比

昨天在看客户端代码时看到缓存用了List,直觉告诉我应该替换成HashSet,尼玛 一个O(N),一个O(1),不用直觉都知道用哪个。

而有同事说性能不一定好,好嘛,我们来个perfonmance test.

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

namespace listtest
{
    class Program
    {
        static void Main(string[] args)
        {
            int times = 10000000;


            for (int listSize = 1; listSize < 10; listSize++)
            {
                List list = new List();
                HashSet hashset = new HashSet();

                for (int i = 0; i < listSize; i++)
                {
                    list.Add("string" + i.ToString());
                    hashset.Add("string" + i.ToString());
                }

                Stopwatch timer = new Stopwatch();
                timer.Start();
                for (int i = 0; i < times; i++)
                {
                    list.Remove("string0");
                    list.Add("string0");
                }
                timer.Stop();
                Console.WriteLine(listSize.ToString() + " item LIST strs time: " + timer.ElapsedMilliseconds.ToString() + "ms");


                timer = new Stopwatch();
                timer.Start();
                for (int i = 0; i < times; i++)
                {
                    hashset.Remove("string0");
                    hashset.Add("string0");
                }
                timer.Stop();
                Console.WriteLine(listSize.ToString() + " item HASHSET strs time: " + timer.ElapsedMilliseconds.ToString() + "ms");
                Console.WriteLine();
            }


            for (int listSize = 1; listSize < 50; listSize += 3)
            {
                List list = new List();
                HashSet hashset = new HashSet();

                for (int i = 0; i < listSize; i++)
                {
                    list.Add(new object());
                    hashset.Add(new object());
                }

                object objToAddRem = list[0];

                Stopwatch timer = new Stopwatch();
                timer.Start();
                for (int i = 0; i < times; i++)
                {
                    list.Remove(objToAddRem);
                    list.Add(objToAddRem);
                }
                timer.Stop();
                Console.WriteLine(listSize.ToString() + " item LIST objs time: " + timer.ElapsedMilliseconds.ToString() + "ms");



                timer = new Stopwatch();
                timer.Start();
                for (int i = 0; i < times; i++)
                {
                    hashset.Remove(objToAddRem);
                    hashset.Add(objToAddRem);
                }
                timer.Stop();
                Console.WriteLine(listSize.ToString() + " item HASHSET objs time: " + timer.ElapsedMilliseconds.ToString() + "ms");
                Console.WriteLine();
            }

            Console.ReadLine();
        }
    }
}
 
  
C#内List VS hashset性能对比_第1张图片


可以明显看出来,hashset的优势,Talk is easy, show the code!

你可能感兴趣的:(C#)