Dictionary 和Hashtable 性能消耗对比

1.Dictionary表示键和值的集合

Dictionary<string,string> 是一个泛型

他的特点是存入对象是需要与[key] 值一一对应的存入该泛型

通过某一个一定的[key] 去找到对应的值

2.哈希表(HashTable)表示键/值对的集合

其中key 通常可用来快速查找,同时key 是区分大小写;

value 用于存储对应于key 的值。

Hashtable key-value 键值对均为object 类型,所以Hashtable 可以支持任何类型的keyvalue 键值对,任何非 null  对象都可以用作键或值。

在哈希表中添加一个key/ 键值对:HashtableObject.Add(key,value);  

在哈希表中去除某个key/ 键值对:HashtableObject.Remove(key);

从哈希表中移除所有元素: HashtableObject.Clear();

判断哈希表是否包含特定键key  HashtableObject.Contains(key);

3.HashTable Dictionary 的区别:

  (1).HashTable 不支持泛型,而Dictionary 支持泛型。

  (2). Hashtable  的元素属于 Object  类型,所以在存储或检索值类型时通常发生装箱和拆箱的操作,所以你可能需要进行一些类型转换的操作,而且对于int,float 这些值类型还需要进行装箱等操作,非常耗时。

  (3). 单线程程序中推荐使用 Dictionary,  有泛型优势且读取速度较快容量利用更充分。多线程程序中推荐使用Hashtable,  默认的Hashtable  允许单线程写入多线程读取 Hashtable  进一步调用 Synchronized()  方法可以获得完全线程安全的类型 Dictionary  非线程安全必须人为使用 lock  语句进行保护效率大减。

(4) 在通过代码测试的时候发现key 是整数型Dictionary 的效率比Hashtable 快,如果key 是字符串型,Dictionary 的效率没有Hashtable 快。

4.测试结果

我用了100000 个数来测试。分别去打印它们添加值,判断是否有值,取值的时间截图如下:



可以看出在单线程下,在key都是string类型的时候,hashtable查找时间很快,
加载和取值速度dictionary和hashtable都差不多,个人测试,如有错误请更正。测试代码如下:(可以试试加入其他类型试试)
[C#]  纯文本查看  复制代码
?
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using GCode;
//using System;
public class dictionaryTest : MonoBehaviour {
     private Stopwatch watching= new Stopwatch();
     private Dictionary< string , int > testD= new Dictionary< string , int >();
     private Hashtable hashtest = new Hashtable ();
     void Start () {
         watching.Start ();
         for ( int i = 0; i < 100000; i++) {
             testD.Add (i.ToString (), i);
         }
         watching.Stop ();
         DebugManager.log( "Dictionary 加载100000个数据的测试时间=" +watching.Elapsed);
 
 
         watching.Reset ();
         watching.Start ();
         for ( int i = 0; i < 100000; i++) {
             hashtest.Add (i.ToString(), i);
         }
         watching.Stop ();
         DebugManager.log( "Hashtable 加载100000个数据的测试时间=" + watching.Elapsed);
 
         watching.Reset ();
         watching.Start ();
         if (testD.ContainsKey ( "5555" )) {
             watching.Stop ();
             DebugManager.log( "Dictionary查找5555测试时间=" + watching.Elapsed);
 
         }
         watching.Reset ();
         watching.Start ();
         if (hashtest.Contains ( "5555" )) {
             watching.Stop ();
             DebugManager.log( "Hashtable查找5555测试时间=" + watching.Elapsed);
         }
 
         watching.Reset ();
         watching.Start ();
         DebugManager.log(( int )hashtest[ "5555" ]);
 
         watching.Stop ();
         DebugManager.log( "Hashtable取值5555时间=" + watching.Elapsed);
 
 
         watching.Reset ();
         watching.Start ();
         DebugManager.log(testD[ "5555" ]);
         watching.Stop ();
         DebugManager.log( "Dictionary取值5555时间=" + watching.Elapsed);
     }
}

[C#]  纯文本查看  复制代码
?
 
01
02
03
04
05
06
07
08
09
10
11
12
using UnityEngine;
using System.Collections;
namespace GCode{
public static class DebugManager  {
     public static bool enableLog = true ;
     public static void log( object content){
             if (enableLog) {
                 Debug.Log (content);
             }
     }
 
     }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值