C#哈希表(HashTable)和Dictionary比较
2021-05-06 12:26
阅读:677
标签:oid ons edm tostring get contains windows console 添加
添加数据时Hashtable快。频繁调用数据时Dictionary快。
Dictionary
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WindowsFormsApp1 { public class Student { private string _name; private int _age; public string Name { get => _name; set => _name = value; } public int Age { get => _age; set => _age = value; } } }
private void button1_Click(object sender, EventArgs e) { Stopwatch sw = new Stopwatch(); Hashtable hashtable = new Hashtable(); Dictionaryint, Student> dict = new Dictionaryint, Student>(); int countNum = 1000000; sw.Start(); for (int i = 0; i ) { Student s = new Student(); s.Name = "n" + i.ToString(); s.Age = i; hashtable.Add(i, s); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); //输出: 817 sw.Restart(); for (int i = 0; i ) { Student s = new Student(); s.Name = "n" + i.ToString(); s.Age = i; dict.Add(i, s); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); //输出: 859 sw.Restart(); for (int i = 0; i ) { hashtable.ContainsKey(i); Student s1 =(Student)hashtable[i]; string str1 = s1.Name; } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); //输出: 137 sw.Restart(); for (int i = 0; i ) { dict.ContainsKey(i); string str=dict[i].Name; } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); //输出: 51 }
C#哈希表(HashTable)和Dictionary比较
标签:oid ons edm tostring get contains windows console 添加
原文地址:http://www.cnblogs.com/ike_li/p/7660462.html
上一篇:窗体间的参数传递
下一篇:window自动备件软件
文章来自:搜素材网的编程语言模块,转载请注明文章出处。
文章标题:C#哈希表(HashTable)和Dictionary比较
文章链接:http://soscw.com/essay/83202.html
文章标题:C#哈希表(HashTable)和Dictionary比较
文章链接:http://soscw.com/essay/83202.html
评论
亲,登录后才可以留言!