.NET 2.0中Hashtable快速查找的方法
2009-03-23 22:40
423 查看
一般来说我们都是用 Hashtable 的 ContainsKey 方法来查找 Hashtable 中是否存在某个键值然后读取他,但是这个方法并不是效率最好的方法。比较好的方法是直接读取键值然后判断这个对象是否为 null 然后读取。两种代码分别如下:
以下为引用的内容: 一般慢速的方法:if (objHash.ContainsKey(keyValue)) { strValue=(String)objHash[keyValue]; } 而快速的方法是:Object objValue=objHash[keyValue]; if (objValue!=null) { strValue=(String)objValue; } 两种方法的速度经过测试能差一倍左右。下面是测试代码: Hashtable objHash = new Hashtable(); for (Int32 intI = 0; intI < 1000; intI++) { objHash.Add("Key_" + intI.ToString(), "Value_" + intI.ToString()); } String strValue = String.Empty; Stopwatch timer = new Stopwatch(); timer.Start(); for (Int32 intI = 0; intI < 1000; intI++) { Object objValue = objHash["Key_" + intI.ToString()]; if (objValue != null) { strValue = (String)objValue; } } timer.Stop(); Console.WriteLine("Execution time was {0:F1} microseconds.", timer.Elapsed.Ticks / 10m); timer.Reset(); timer.Start(); for (Int32 intI = 0; intI < 1000; intI++) { if (objHash.ContainsKey("Key_" + intI.ToString())) { strValue = (String)objHash["Key_" + intI.ToString()]; } } timer.Stop(); Console.WriteLine("Execution time was {0:F1} microseconds.", timer.Elapsed.Ticks / 10m); timer.Reset(); |
相关文章推荐
- NET 2.0中Hashtable快速查找的方法
- Hashtable快速查找的方法
- Hashtable快速查找的方法
- .net Repeater无数据时显示"no result" 快速解决方法
- .NET 2.0 转4.0 执行SqlLite操作时报错的解决方法
- 探讨.NET 2.0中Tuple的实现方法
- .Net 中用查找窗体标题的方法检查WinForm应用是否重复运行
- myeclipse中快速查找类、方法
- 如何去掉DataTable中的重复行(新增.net 2.0中最新解决方法---简便) (转)
- 基于.Net 2.0 (C# 2.0, ASP.NET 2.0)的快速开发框架设计NBear V1.8.1 [开源]
- eclipse快速查找一个变量、方法或者类被引用的地方
- 在.NET中发送邮件,使用回执功能中遇到问题的解决方法[在NOTES服务器上,C#,.NET2.0环境]
- win2003 sp2+iis 6.0上部署.net 2.0和.net 4.0网站的方法
- RDIFramework.NET ━ .NET快速信息化系统开发框架-第3章 软件安装、配置、运行方法
- .NET 2.0中的Eval使用方法总结(未完成,续)
- 【Vegas原创】更加快速的方法:HashTable遍历TreeView
- MSTAR虚函数表(以IBitmap为例),以及快速查找函数实现的方法:FUNCTBL(IBitmap
- IIS和.NET(1.1/2.0)的安装顺序及错误解决方法
- (转)数据记录的快速查找方法
- SAS应用技巧-快速在CATALOG的SCL中查找某字符串的方法