`
poson
  • 浏览: 348807 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Dictionary比hashSet快?

阅读更多
class Program 
{
static void Main(string[] args)
{
HashSet<string> hash=new HashSet<string> ();
//url列表 
StreamReader reader = new StreamReader( "AccessURL.txt", true);
string url = "";
List<string> list = new List<string>();
while (!reader.EndOfStream)
{
url = reader.ReadLine();
list.Add(url); 
}
reader.Close();
Dictionary<string,bool> dic=new Dictionary<string,bool> ();
DateTime time1 = DateTime.Now;
foreach(string s in list)
{
hash.Add(s);
}
DateTime time2 = DateTime.Now;
TimeSpan t = new TimeSpan(time2.Ticks - time1.Ticks);
Console.WriteLine("hash add: " + t.Ticks.ToString());
time1 = DateTime.Now;
foreach(string s in list)
{
dic.Add(s,true);
}
time2 = DateTime.Now;
t = new TimeSpan(time2.Ticks - time1.Ticks);
Console.WriteLine("dicadd: "+t.Ticks.ToString());
time1 = DateTime.Now;
foreach (string s in list)
{
hash.Contains(s);
}
time2 = DateTime.Now;
t = new TimeSpan(time2.Ticks - time1.Ticks);
Console.WriteLine("hashfind: " + t.Ticks.ToString());
time1 = DateTime.Now;
foreach (string s in list)
{
dic.ContainsKey(s);
}
time2 = DateTime.Now;
t = new TimeSpan(time2.Ticks - time1.Ticks);
Console.WriteLine("dic find: " + t.Ticks.ToString());
Console.ReadLine();
}
}
运行结果:
hash add: 625000
dicadd: 156250
hashfind: 468750
dic find: 156250
(1)Dictionary适合数据非常有层次性的东西。特别是具有目录结构的东西。Dictionary本身的含义就是词典嘛。 
(2)保存URL的时候,应该Dictionary比hashSet快。 
保存大量的md5码的时候,Dictionary比hashSet快。 

 

分享到:
评论

相关推荐

    C# ArrayList、HashSet、HashTable、List、Dictionary的区别详解

    主要介绍了C# ArrayList、HashSet、HashTable、List、Dictionary的区别的相关知识点内容,有需要朋友们参考下。

    HashTable、HashSet和Dictionary的区别点总结

    在本篇文章里小编给大家整理的是关于HashTable、HashSet和Dictionary的区别点,需要的朋友们可以学习下。

    hash table spell checking

    Remember, class Dictionary is a type of HashSet, so use the inherited methods accordingly. 3. Next, complete the hash function encapsulated in class hash_function in dictionary.h. 4. Then, finish ...

    数据结构作业Hash表

    Remember, class Dictionary is a type of HashSet, so use the inherited methods accordingly. 3. Next, complete the hash function encapsulated in class hash_function in dictionary.h. 4. Then, finish ...

    Data-Structure:简单的数据结构,包括HashSet,HashMap,Heap,Red-Black Tree等

    数据结构 简单的数据结构,包括HashSet,HashMap,Heap,Red-Black Tree等。

    JavaSE 笔试 精华

    Collection List LinkedList ArrayList Vector Stack Set HashSet Map HashMap Dictionary Hashtable Comparetor 2. Vector和ArrayList、LinkedList区别? Hashtable 和 HashMap之间的区别 LinkedList内部以链表...

    word detection

    Dictionary, Dictionary, int&gt;&gt; words = new Dictionary, Dictionary, int&gt;&gt;(); Dictionary, int&gt; freq = new Dictionary, int&gt;(); Dictionary, double&gt; ps = new Dictionary, double&gt;(); Regex regSplit = ...

    Leetcode最小费用旅行-Codefun:码趣

    Dictionary 内部实现 集合 HashSet 内部实现 HashSet内部基础结构是数组,构造参数为零时不会分配内存 Hashtable Hashtable 内部实现 基础数据结构是bucket, bucket数组来存储数据 new Hashtable(): capacity 初始化...

    DataStructureJava:主要数据结构——java中的简单实现

    interface Map&lt;Key&gt; TreeMap&lt;K&gt; HashMap&lt;K&gt; LinkedHashMap&lt;K&gt; Hashtable&lt;K&gt; extends Dictionary&lt;K&gt; - JDK 1.0 接口 Set TreeSet HashSet 树特里================== 问题: 如何使用两个堆栈实现队列? 答案:...

    multiplex.js:针对JavaScript的LINQ

    Dictionary -键/值对的集合。 Collection -强类型集合。 ReadOnlyCollection只读包装器集合。 HashSet不含重复元素的高性能值集。 SortedList按键排序的键/值对的集合。 LinkedList双链表。 Queue -对象的...

    C#利用正则判断输入是否为纯数字、容器类

    开始直接输出在C#定义好的数据字典Dictionary,这就是Java与Python的HashMap,之后定义一个存int的List,让用户无限输入这个List的元素,输入到#则停止输入,在输入的过程中遇到不是纯输入,则拒绝这个输入。...

    Java开发技术大全 电子版

    11.2.4哈希集合(HashSet)使用示例343 11.2.5哈希映射类(HashMap)使用示例347 11.2.6有序树(TreeSet)使用示例349 11.2.7有序树映射类(TreeMap)使用示例353 11.2.8枚举(Enum)使用示例355 11.2.9枚举集...

Global site tag (gtag.js) - Google Analytics