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

推荐订阅源

博客园 - 三生石上(FineUI控件)
月光博客
月光博客
人人都是产品经理
人人都是产品经理
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence
Vercel News
Vercel News
MyScale Blog
MyScale Blog
爱范儿
爱范儿
博客园 - 司徒正美
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
H
Help Net Security
Last Week in AI
Last Week in AI
阮一峰的网络日志
阮一峰的网络日志
酷 壳 – CoolShell
酷 壳 – CoolShell
L
LangChain Blog
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
宝玉的分享
宝玉的分享
博客园 - 聂微东
云风的 BLOG
云风的 BLOG
J
Java Code Geeks
博客园 - 叶小钗
D
Docker

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

        }