关于Hashtable与Dictionary性能的讨论。
来源:互联网 发布:mysql教材推荐 编辑:程序博客网 时间:2024/05/12 18:16
我个人是觉得,无论什么时候,都应该使用Dictionary<K,V>,理由如下:
1、Dic是类型安全的,这有助于我们写出更健壮更具可读性的代码,而且省却我们强制转化的麻烦。这个相信大家都明白。
2、Dic是泛行的,当K或V是值类型时,其速度远远超过Hashtable。这个大家对值类型与引用类型有所了解的话也会明白。
3、如果K和V都是引用类型,如eaglet所测,Hashtable比Dic更快,这里我要指出,eaglet所做的测试是有问题的。原因在于Hashtable与Dic采用的是不同的数据结构。eaglet的“Dictionary 由于在Hashtable基础上封装了一层”这个说法是不对的。
具体我也不讲了,因为有人(Angel Lucifer)已经讲得很清楚了,引用如下:
http://www.cnblogs.com/lucifer1982/archive/2008/06/18/1224319.html
http://www.cnblogs.com/lucifer1982/archive/2008/07/03/1234431.html
Hashtable在指定capacity参数时,它并不只开出capacity个槽的内存空间,而是开出比 capacity / 0.72(默认装填因子) 大的最小素数个槽的空间;而Dic在指定capacity时,是开出 比capacity 大的最小素数个槽的空间。因此可以看到,楼主虽然都指定capacity为10万,而实际上Hashtable的槽的总数远远大于Dic的槽的总数,也就是占用的内存远远大于Dic,因此,如此测试是不公平不公正的,如要公平公正的测试,则应该把Dic的capacity指定为 10万/0.72,请大家再测试其性能。
下表是我测试的Insert的性能。(机器是老爷机了,跑的太慢了)
4、楼主的测试不包括扩容所占的开销,实际上,Dic的扩容开销远远小于Hashtable,而我们知道,扩容是极为消耗性能的。
总上所述,我认为应该始终使用Dictionary<K, V>,即使要用Hashtable了,也可以用Dictionary<object, object>来替代。
测试代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace StringDictionaryPerformance
{
class Program
{
static Random _Rand = new Random();
static Hashtable _Hashtable;
static Dictionary<string, object> _Dictionary;
static object _object;
static string GetRandString(int length)
{
StringBuilder str = new StringBuilder();
for (int i = 0; i < length; i++)
{
str.Append((char)_Rand.Next(32, 128));
}
return str.ToString();
}
static List<string> GetTestStrings(int length, int number)
{
List<string> retVal = new List<string>(number);
for (int i = 0; i < number; i++)
{
retVal.Add(GetRandString(length));
}
return retVal;
}
static void TestInsert(List<string> strings, bool sort)
{
if (sort)
{
strings.Sort();
}
Console.WriteLine(string.Format("TestInsert string length = {0} count of strings = {1} sort={2}",
strings[0].Length, strings.Count, sort));
Stopwatch stopWatch = new Stopwatch();
Console.WriteLine("Begin Hashtable");
_Hashtable = new Hashtable(strings.Count);
stopWatch.Reset();
stopWatch.Start();
foreach (string item in strings)
{
if (!_Hashtable.ContainsKey(item))
{
_Hashtable.Add(item, _object);
}
}
stopWatch.Stop();
Console.WriteLine(string.Format("ElapsedMilliseconds = {0} ms", stopWatch.ElapsedMilliseconds));
Console.WriteLine("Begin Dictoinary");
int tempCount = (int)(strings.Count / 0.72);
_Dictionary = new Dictionary<string, object>(tempCount);
stopWatch.Reset();
stopWatch.Start();
foreach (string item in strings)
{
if (!_Dictionary.ContainsKey(item))
{
_Dictionary.Add(item, _object);
}
}
stopWatch.Stop();
Console.WriteLine(string.Format("ElapsedMilliseconds = {0} ms", stopWatch.ElapsedMilliseconds));
}
static void TestFind(List<string> strings, bool sort)
{
Console.WriteLine(string.Format("TestFind string length = {0} count of strings = {1} sort={2}",
strings[0].Length, strings.Count, sort));
Stopwatch stopWatch = new Stopwatch();
Console.WriteLine("Begin Hashtable");
stopWatch.Reset();
stopWatch.Start();
foreach (string item in strings)
{
if (_Hashtable[item] != _object)
{
Console.WriteLine("Error!");
}
}
stopWatch.Stop();
Console.WriteLine(string.Format("ElapsedMilliseconds = {0} ms", stopWatch.ElapsedMilliseconds));
Console.WriteLine("Begin Dictoinary");
stopWatch.Reset();
stopWatch.Start();
foreach (string item in strings)
{
if (_Dictionary[item] != _object)
{
Console.WriteLine("Error!");
}
}
stopWatch.Stop();
Console.WriteLine(string.Format("ElapsedMilliseconds = {0} ms", stopWatch.ElapsedMilliseconds));
}
static void Main(string[] args)
{
List<string> strings;
strings = GetTestStrings(16, 100000);
TestInsert(strings, false);
TestFind(strings, false);
TestInsert(strings, true);
TestFind(strings, true);
strings = GetTestStrings(128, 100000);
TestInsert(strings, false);
TestFind(strings, false);
TestInsert(strings, true);
TestFind(strings, true);
strings = GetTestStrings(1024, 100000);
TestInsert(strings, false);
TestFind(strings, false);
TestInsert(strings, true);
TestFind(strings, true);
}
}
}
- 关于Hashtable与Dictionary性能的讨论
- 关于Hashtable与Dictionary性能的讨论。
- 关于Hashtable与Dictionary性能的讨论
- C#Hashtable与Dictionary性能
- C#Hashtable与Dictionary性能
- C#Hashtable与Dictionary性能
- C#Hashtable与Dictionary性能
- C#Hashtable与Dictionary性能
- 关于Hashtable与Dictionary差异
- Hashtable与Dictionary的异同
- hashtable 与 dictionary的区别
- 转载:Dictionary与HashTable的比较
- C#中hashtable 与 dictionary的区别
- Dictionary 与 Hashtable 的速度比较
- HashTable与Dictionary的区别说明
- 浅谈Hashtable与Dictionary的异同
- Dictionary 与 Hashtable
- GetHashCode 与HashTable ,Dictionary
- wpf 控件的抖动效果
- C语言的inline
- 我的其他博客地址
- 新浪微博OAuth授权的Java实现
- 【博客话题】技术之路上的人和事
- 关于Hashtable与Dictionary性能的讨论。
- 脚踏实地,避免成为浮躁贪婪的人
- WebService CXF学习(进阶篇1):SOAP讲解
- 支持BMP文件拖放换肤 QQ2011风格(MFC)
- GCC 编译使用动态链接库和静态链接库
- Linux的那些事
- 跨越金字塔,做你自己的老板
- c++ 函数返回引用
- windows版本检测函数