List,Dictionary,HashSet各个方法性能比较

今天在做项目的过程中,发现了算法的性能瓶颈,因为算法用了  public Dictionary<string, List<cKnowledgeLi>> cchooseli = new Dictionary<string, List<cKnowledgeLi>>();其中cKnowledgeLi是大对象,所以在对Dictionary的各种方法的操作性能上存在问题,所以突发奇想,想比对下各个存储之间的各个方法操作的性能,进而优化。废话不多说,直接上代码,

const int COUNT = 10000;

            HashSet<int> hashSetOfInts = new HashSet<int>();

            Stopwatch stopWatch = new Stopwatch();

 

            Console.WriteLine("对10000次进行处理:");

            stopWatch.Start();

            List<int> listOfInts = new List<int>();

            for (int i = 0; i < COUNT; i++)

            {

                listOfInts.Add(i);

            }

            Console.WriteLine("集合添加:" + stopWatch.Elapsed);

            stopWatch.Reset();

 

            Console.WriteLine("-------------------------------------------");

            Console.WriteLine();

 

            stopWatch.Start();

            int value =0;

            for (int i = 0; i < listOfInts.Count; i++)

            {

                value = listOfInts[i];

            }

            Console.WriteLine("集合For循环:" + stopWatch.Elapsed);

            stopWatch.Reset();

 

            Console.WriteLine("-------------------------------------------");

            Console.WriteLine();

 

            stopWatch.Start();

            int value1 = 0;

            var enumerator = listOfInts.GetEnumerator();

            while (enumerator.MoveNext())

            {

                value1 = enumerator.Current;

              

            }

            Console.WriteLine("集合迭代器:" + stopWatch.Elapsed);

            stopWatch.Reset();

 

            Console.WriteLine("-------------------------------------------");

            Console.WriteLine();

 

            stopWatch.Start();

            for (int i = 0; i < COUNT; i++)

            {

                listOfInts.Contains(i);

            }

            stopWatch.Stop();

 

            Console.WriteLine("集合包含:" + stopWatch.Elapsed);

            stopWatch.Reset();

 

            Console.WriteLine("-------------------------------------------");

            Console.WriteLine();

 

            stopWatch.Start();

            Dictionary<string, string> domainMap = new Dictionary<string, string>();

            for (int i = 0; i < COUNT; i++)

            {

                domainMap.Add(i.ToString(), i.ToString());

            }

            Console.WriteLine("字典添加:" + stopWatch.Elapsed);

            stopWatch.Reset();

 

            Console.WriteLine("-------------------------------------------");

            Console.WriteLine();

 

 

            stopWatch.Start();

            string value2 = "";

 

            foreach (string key in domainMap.Keys)

            {

                value2 = domainMap[key];

            }

            Console.WriteLine("字典For循环:" + stopWatch.Elapsed);

            stopWatch.Reset();

 

            Console.WriteLine("-------------------------------------------");

            Console.WriteLine();

 

            stopWatch.Start();

            string value3 = "";

            var enumerator1 = domainMap.GetEnumerator();

            while (enumerator1.MoveNext())

            {

                value3 = enumerator1.Current.Key;

 

            }

            Console.WriteLine("字典迭代器:" + stopWatch.Elapsed);

            stopWatch.Reset();

 

            Console.WriteLine("-------------------------------------------");

            Console.WriteLine();

 

            stopWatch.Start();

 

            for (int i = 0; i < COUNT; i++)

            {

                domainMap.ContainsKey(i.ToString());

            }

            stopWatch.Stop();

            Console.WriteLine("字典ContainsKey:" + stopWatch.Elapsed);

            stopWatch.Reset();

 

            Console.WriteLine("-------------------------------------------");

            Console.WriteLine();

 

 

            stopWatch.Start();

            for (int i = 0; i < COUNT; i++)

            {

                hashSetOfInts.Add(i);

            }

            stopWatch.Stop();

            Console.WriteLine("HashSet添加:" + stopWatch.Elapsed);

            stopWatch.Reset();

 

            Console.WriteLine("-------------------------------------------");

            Console.WriteLine();

 

 

            stopWatch.Start();

            int value4 =0;

 

            foreach (var key in hashSetOfInts)

            {

                value4 = key;

            }

            Console.WriteLine("HashSetFor循环:" + stopWatch.Elapsed);

            stopWatch.Reset();

 

            Console.WriteLine("-------------------------------------------");

            Console.WriteLine();

 

            stopWatch.Start();

            int value5 = 0;

            var enumerator2 = hashSetOfInts.GetEnumerator();

            while (enumerator2.MoveNext())

            {

                value5 = enumerator2.Current;

 

            }

            Console.WriteLine("HashSet迭代器:" + stopWatch.Elapsed);

            stopWatch.Reset();

 

            Console.WriteLine("-------------------------------------------");

            Console.WriteLine();

 

 

 

 

 

            stopWatch.Start();

            for (int i = 0; i < COUNT; i++)

            {

                hashSetOfInts.Contains(i);

            }

            stopWatch.Stop();

            Console.WriteLine("HashSet包含:" + stopWatch.Elapsed);

            stopWatch.Reset();

 

         

            Console.WriteLine("-------------------------------------------");

            Console.WriteLine();

 

 

比较效果图:

 

 

 

 

转载于:https://www.cnblogs.com/yenantian/p/7512134.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值