Faster Hashtable lookups in .NET
2007-03-06 10:13
197 查看
I almost forget where the original source is from.
Hashtables are very convenient data structures however they are not very fast and it is very easy to make them even slower by using more logical, but ultimately much slower approach, consider the code below:
The difference in performance is about 2 times: the reason is that checking whether a hashtable contains an item actually has the same or almost the same cost as retrieving value from it or getting null if it is not present: this would not work if possible value of a key can actually be null, but in many cases using faster version will improve performance nicely, so long as you are not bottlenecked elsewhere!
Hashtables are very convenient data structures however they are not very fast and it is very easy to make them even slower by using more logical, but ultimately much slower approach, consider the code below:
Hashtable oHash=new Hashtable();
string sKey=“key”; oHash[sKey]=“value”;
string sValue=“”;
// slow! if(oHash.Contains(sKey)) sValue=(string)oHash[sKey]; // clear up value sValue=“”;
// much faster object oValue=oHash[sKey];
if(oValue!=null) sValue=(string)oValue;
The difference in performance is about 2 times: the reason is that checking whether a hashtable contains an item actually has the same or almost the same cost as retrieving value from it or getting null if it is not present: this would not work if possible value of a key can actually be null, but in many cases using faster version will improve performance nicely, so long as you are not bottlenecked elsewhere!
相关文章推荐
- Better, Faster, Lighter Programming in .NET
- Container.DataItem is faster than Eval in ASP.NET
- mxnet-model-server 运行出现 libjemalloc.so.2: cannot allocate memory in static TLS block
- Referral or Affiliate Code tracking Module in ASP.NET
- Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketT
- ASP.Net MVC Entity Framework Error – More than one context type was found in the assembly ‘ProjectNa
- %5c类的漏洞 in asp/asp.NET
- 碰到这个SB错误,'Taglist: Exuberant ctags (http://ctags.sf.net) not found in PATH. Plugin is not loaded.点办
- java.lang.IncompatibleClassChangeError: class net.sf.cglib.core.DebuggingClassWriter has interface o
- asp.net分页,Effective paging with List View control in ASP.NET
- Sams Teach Yourself Visual Studio .NET 2003 in 21 Days
- Encoding in .net
- Essential ASP.NET With Examples in C#
- Pro WPF: Windows Presentation Foundation in .NET 3.0
- Public Key RSA Encryption in C# .NET
- [C#/.NET]how to implement web application localization in .net 4.0
- Active Directory Authentication in ASP.NET MVC 5 with Forms Authentication and Group-Based Authorization
- AOP in dotnet :AspectCore的参数拦截支持
- SPPNet论文翻译-空间金字塔池化Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition
- FAILED: Error in metadata: MetaException(message:Got exception: java.net.ConnectException Call to ha