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

推荐订阅源

罗磊的独立博客
小众软件
小众软件
The Cloudflare Blog
博客园 - 【当耐特】
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
酷 壳 – CoolShell
酷 壳 – CoolShell
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Visual Studio Blog
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
美团技术团队
S
SegmentFault 最新的问题
宝玉的分享
宝玉的分享
博客园 - 叶小钗
月光博客
月光博客
Apple Machine Learning Research
Apple Machine Learning Research
T
Tailwind CSS Blog
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
J
Java Code Geeks
Y
Y Combinator Blog
D
Docker
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - 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);

        }