• C# 集合性能比较(代码测试)


    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    
    namespace Table_And_List
    {
    
        public class Program
        {
    
            public static void Main(string[] args)
            {
                int total = 100000;
                int[] x = new int[9999999];
                for (int i = 0; i < x.Length; i++)
                {
                    x[i] = i;
                }
                int[] x2 = new int[] { 6666, 333, 11111, 56764, 33335 };
                int x3 = new Random().Next(total);
                SortedDictionaryTest(x, x2, x3);
                DictionaryTest(x, x2, x3);
                SortedListTest(x, x2, x3);
                HashtableTest(x, x2, x3);
                Console.ReadKey();
            }
    
            private static void SortedListTest(int[] x, int[] x2, int x3)
            {
                var ht = new SortedList();
                Stopwatch watch = new Stopwatch();
                watch.Start();
                foreach (var item in x)
                {
                    ht.Add(item, item);
                }
                watch.Stop();
                Console.WriteLine(string.Format("SortedList添加{0}个元素耗时:{1}ms", x.Length, watch.ElapsedMilliseconds));
                watch.Reset();
                watch.Start();
                foreach (var item in x2)
                {
                    //Console.WriteLine("value:{0},?{1}",item,ht.ContainsKey(item));
                    ht.ContainsValue(item);
                }
                watch.Stop();
                Console.WriteLine(string.Format("SortedList查找在100000个元素中查找随机的{0}个的元素耗时:{1}ms", x2.Length, watch.ElapsedMilliseconds));
                ht.Clear();
            }
            private static void HashtableTest(int[] x, int[] x2, int x3)
            {
                Hashtable ht = new Hashtable();
                Stopwatch watch = new Stopwatch();
                watch.Start();
                foreach (var item in x)
                {
                    ht.Add(item, item);
                }
                watch.Stop();
                Console.WriteLine(string.Format("Hashtable添加{0}个元素耗时:{1}ms", x.Length, watch.ElapsedMilliseconds));
                watch.Reset();
                watch.Start();
                foreach (var item in x2)
                {
                    //Console.WriteLine("value:{0},?{1}",item,ht.ContainsKey(item));
                    //ht.ContainsKey(item);
                    ht.ContainsValue(item);
                }
                watch.Stop();
                Console.WriteLine(string.Format("Hashtable查找在100000个元素中查找随机的{0}个的元素耗时:{1}ms", x2.Length, watch.ElapsedMilliseconds));
                ht.Clear();
            }
    
            private static void DictionaryTest(int[] x, int[] x2, int x3)
            {
                Dictionary<int, int> ht = new Dictionary<int, int>();
                Stopwatch watch = new Stopwatch();
                watch.Start();
                foreach (var item in x)
                {
                    ht.Add(item, item);
                }
                watch.Stop();
                Console.WriteLine(string.Format("Dictionary添加{0}个元素耗时:{1}ms", x.Length, watch.ElapsedMilliseconds));
                watch.Reset();
                watch.Start();
                foreach (var item in x2)
                {
                    //Console.WriteLine("value:{0},?{1}", item, ht.ContainsKey(item));
                    //ht.ContainsKey(item);
                    ht.ContainsValue(item);
                }
                watch.Stop();
                Console.WriteLine(string.Format("Dictionary查找在100000个元素中查找随机的{0}个的元素耗时:{1}ms", x2.Length, watch.ElapsedMilliseconds));
                ht.Clear();
            }
    
            private static void SortedDictionaryTest(int[] x, int[] x2, int x3)
            {
                SortedDictionary<int, int> ht = new SortedDictionary<int, int>();
                Stopwatch watch = new Stopwatch();
                watch.Start();
                foreach (var item in x)
                {
                    ht.Add(item, item);
                }
                watch.Stop();
                Console.WriteLine(string.Format("SortedDictionary添加{0}个元素耗时:{1}ms", x.Length, watch.ElapsedMilliseconds));
                watch.Reset();
                watch.Start();
                foreach (var item in x2)
                {
                    //Console.WriteLine("value:{0},?{1}", item, ht.ContainsKey(item));
                    //ht.ContainsKey(item);
                    ht.ContainsValue(item);
                }
                watch.Stop();
                Console.WriteLine(string.Format("SortedDictionary查找在100000个元素中查找随机的{0}个的元素耗时:{1}ms", x2.Length, watch.ElapsedMilliseconds));
                ht.Clear();
            }
        }
    }

  • 相关阅读:
    一次郁闷的项目部署经历
    一道面试题的求解
    一次惨痛的教训
    小地方,大郁闷001
    WPF相对资源的访问
    VS2008安装时提示磁盘空间不够的解决办法
    WPFSlider 控件的使用
    关于asp.net大文件上传和进度条实现的学习(1)
    DataList的一次分页困惑
    关于android的XML的解析
  • 原文地址:https://www.cnblogs.com/mingxuantongxue/p/3911316.html
Copyright © 2020-2023  润新知