zoukankan      html  css  js  c++  java
  • Hashtable与Dictionary比较

      项目需要存储Tcp连接对象,考虑使用Hashtable或者Dictionary存储。Hashtable在查询方面有优势,Dictionary在确定类型下不需要拆箱与装箱有优势。于是,写了个demo对两个存储对象进行了插入、查询、删除、遍历的速度比较。

            static Hashtable hashtable = new Hashtable();
            static Dictionary<string, ConnectedClient> keyValuePairs = new Dictionary<string, ConnectedClient>();
    
            static void Init()
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                for(int i = 0; i < 100000; i++)
                {
                    ConnectedClient connectedClient = new ConnectedClient(new TcpClient());
                    hashtable.Add(i.ToString(),connectedClient);
                }
                stopwatch.Stop();
                Console.WriteLine(string.Format("Hashtable插入100000耗时{0}。", stopwatch.ElapsedTicks));
                stopwatch.Restart();
                for (int i = 0; i < 100000; i++)
                {
                    ConnectedClient connectedClient = new ConnectedClient(new TcpClient());
                    keyValuePairs.Add(i.ToString(), connectedClient);
                }
                stopwatch.Stop();
                Console.WriteLine(string.Format("Dictionary插入100000耗时{0}。", stopwatch.ElapsedTicks));
    
                stopwatch.Restart();
                var result = (ConnectedClient)hashtable["70000"];
                stopwatch.Stop();
                Console.WriteLine(string.Format("Hashtable搜索一条耗时{0}。", stopwatch.ElapsedTicks));
    
                stopwatch.Restart();
                result = keyValuePairs["70000"];
                stopwatch.Stop();
                Console.WriteLine(string.Format("Dictionary搜索一条耗时{0}。", stopwatch.ElapsedTicks));
    
                stopwatch.Restart();
                hashtable.Add("9000000", new ConnectedClient(new TcpClient())); ;
                stopwatch.Stop();
                Console.WriteLine(string.Format("Hashtable插入一条耗时{0}。", stopwatch.ElapsedTicks));
    
                stopwatch.Restart();
                keyValuePairs.Add("9000000", new ConnectedClient(new TcpClient())); ;
                stopwatch.Stop();
                Console.WriteLine(string.Format("Dictionary插入一条耗时{0}。", stopwatch.ElapsedTicks));
    
                stopwatch.Restart();
                hashtable.Remove("9000000");;
                stopwatch.Stop();
                Console.WriteLine(string.Format("Hashtable删除一条耗时{0}。", stopwatch.ElapsedTicks));
    
                stopwatch.Restart();
                keyValuePairs.Remove("9000000");;
                stopwatch.Stop();
                Console.WriteLine(string.Format("Dictionary删除一条耗时{0}。", stopwatch.ElapsedTicks));
    
                stopwatch.Restart();
                foreach (var key in hashtable.Keys)
                {
                    ((ConnectedClient)hashtable[key]).outCount--;
                }
                Console.WriteLine(string.Format("Hashtable遍历耗时{0}。", stopwatch.ElapsedTicks));
                stopwatch.Stop();
    
                stopwatch.Restart();
                foreach (var key in keyValuePairs.Keys)
                {
                    keyValuePairs[key].outCount--;
                }
                Console.WriteLine(string.Format("Dictionary遍历耗时{0}。", stopwatch.ElapsedTicks));
                stopwatch.Stop();
    
                Console.ReadLine();
            }
    

      

  • 相关阅读:
    Firefox浏览器怎么安装adobe flash player插件
    uploadify在火狐下上传不了的解决方案,java版(Spring+SpringMVC+MyBatis)详细解决方案...
    thinkphp模版调用函数方法
    Thinkphp模板中函数的使用
    60.0.1(64位)windows版 uploadify使用有问题
    一起谈.NET技术,异步调用与多线程的区别 狼人:
    一起谈.NET技术,Silverlight中使用递归构造关系图 狼人:
    一起谈.NET技术,ASP.NET Routing对请求的处理方式 狼人:
    一起谈.NET技术,闲话“多线程” 狼人:
    一起谈.NET技术,利用.NET Framework4.0的源代码调试你的应用程序 狼人:
  • 原文地址:https://www.cnblogs.com/yuekong2010/p/9274051.html
Copyright © 2011-2022 走看看