惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tailwind CSS Blog
The GitHub Blog
The GitHub Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog
Microsoft Security Blog
Microsoft Security Blog
Stack Overflow Blog
Stack Overflow Blog
量子位
Martin Fowler
Martin Fowler
月光博客
月光博客
P
Proofpoint News Feed
博客园_首页
Y
Y Combinator Blog
I
InfoQ
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
Visual Studio Blog
H
Help Net Security
U
Unit 42
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 司徒正美
MongoDB | Blog
MongoDB | Blog
F
Fortinet All Blogs
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - liufu627

使用option 链式调用来代替异步方法幂调用 Rust 各种情况下如何修改变量 Rust 单链表的实现 Closure move 数值类型与引用类型的区别 rust 使用泛型来完成多态 # Rust异步网络编程 Rust学习 红黑树新解(删除) 红黑树新解(插入) WCF快速創建 dropdownlist - liufu627 - 博客园 ArcGIS安装 泛型OraHelper - liufu627 - 博客园 让SendKeys支持空格键 - liufu627 - 博客园 标点符号的英文说法 PDA datagrid问题 日期格式化 - liufu627 - 博客园 JSON.net的使用 关于C#测试Oracle数据库链接的问题
Hashtable 让哈希表顺序输出。
liufu627 · 2007-10-24 · via 博客园 - liufu627

class QueueHashtable<TKey, TValue>
    {
        List<TKey> _keys = new List<TKey>();
        Hashtable ht = new Hashtable();

        public ICollection Keys
        {
            get
            {
                return _keys;
            }
        }
        public ICollection Values
        {
            get
            {
                return ht.Values;
            }
        }      

        public void Clear()
        {
            _keys.Clear();
            ht.Clear();
        }
        /// <summary>
        /// 根据键名得到键值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public virtual TValue this[TKey key]
        {
            get
            {
                return (TValue) ht[key];
            }
            set
            {
                ht[key] = value;
            }

        }
        public void Add(TKey key, TValue value)
        {
            ht.Add(key, value);
            _keys.Add(key);
        }

        public virtual bool ContainsKey(TKey key)
        {
            return ht.ContainsKey(key);
        }

        public virtual bool ContainsValue(TValue value)
        {
            return ht.ContainsValue(value);
        }      
        public virtual void Remove(TKey key)
        {
            ht.Remove(key);
        }
    }

//执行

 const int C_Count = 1000000;
            DateTime start = DateTime.Now;
            TimeSpan ts;

            //begin
            Console.WriteLine();

            System.Collections.Hashtable ht = new System.Collections.Hashtable();
            //add 10000

            start = DateTime.Now;
            for (int i = 0; i < C_Count; i++)
            {
                ht.Add(i, i);
            }
            ts = DateTime.Now - start;
            Console.WriteLine("  System Hashtable add  {0} elements cost '{1}' (ms)", C_Count, ts.TotalMilliseconds);

            QueueHashtable<int, int> qu = new QueueHashtable<int, int>();
            start = DateTime.Now;
            for (int i = 0; i < C_Count; i++)
            {
                qu.Add(i, i);
            }
            ts = DateTime.Now - start;
            Console.WriteLine(" HashtableQueue<int, int>   add  {0} elements cost '{1}' (ms)", C_Count, ts.TotalMilliseconds);

            start = DateTime.Now;
            foreach (int key in ht.Keys)
                ;
            ts = DateTime.Now - start;
            Console.WriteLine("  int key System Hashtable foreach  {0} elements cost '{1}' (ms)", C_Count, ts.TotalMilliseconds);

            start = DateTime.Now;
            foreach (int key in t.Keys)
                ;
            ts = DateTime.Now - start;
            Console.WriteLine("  HashtableQueue<int, int>  foreach  {0} elements cost '{1}' (ms)", C_Count, ts.TotalMilliseconds);

            Console.WriteLine();


            //comment
            //Hashtable t = new Hashtable();

            //start = DateTime.Now;
            //for (int i = 0; i < C_Count; i++)
            //{
            //    t.Add(i, i);
            //}
            //ts = DateTime.Now - start;
            //Console.WriteLine(" My Hashtable  add  {0} elements cost '{1}' (ms)", C_Count, ts.TotalMilliseconds);

            //start = DateTime.Now;
            //foreach (int key in t.Keys)
            //    ;
            //ts = DateTime.Now - start;
            //Console.WriteLine("  My Hashtable foreach  {0} elements cost '{1}' (ms)", C_Count, ts.TotalMilliseconds);

        }