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<string> list = new List<string>();
                HashSet<string> hashset = new HashSet<string>();

                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<object> list = new List<object>();
                HashSet<object> hashset = new HashSet<object>();

                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();
        }
    }
}



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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值