Dictionary加速查询(TryGetValue)
生活随笔
收集整理的這篇文章主要介紹了
Dictionary加速查询(TryGetValue)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在程序中常會有這樣的代碼。
多了可能會影響效率。
看來沒有什么問題。
但是在實際項目中,這種代碼一般會寫在底層的class中。
它被調用的次數 相當大時,需要優化。
MS.Net2.0如何實現:
FindEntry()被調用了2次!這就導致速度變慢了1倍!
解決方案:估計微軟自己也發現 了,所以在2.0里面封裝了一個新的method:
于是,上面的代碼就可以改寫成:
使用TryGetValue,FindEntry 只調用了一次,同時判斷了有沒有也得到了值。 在程序中常會有這樣的代碼。
多了可能會影響效率。
看來沒有什么問題。
但是在實際項目中,這種代碼一般會寫在底層的class中。
它被調用的次數 相當大時,需要優化。
MS.Net2.0如何實現:
FindEntry()被調用了2次!這就導致速度變慢了1倍!
解決方案:估計微軟自己也發現 了,所以在2.0里面封裝了一個新的method:
于是,上面的代碼就可以改寫成:
使用TryGetValue,FindEntry 只調用了一次,同時判斷了有沒有也得到了值。
多了可能會影響效率。
| Dictionary<Key, Value> dict = new Dictionary<Key, Value>(); ........... if (dict.ContainsKey(key)) { Value value = dict[key]; } |
看來沒有什么問題。
但是在實際項目中,這種代碼一般會寫在底層的class中。
它被調用的次數 相當大時,需要優化。
MS.Net2.0如何實現:
| public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback { public bool ContainsKey(TKey key) { return (this.FindEntry(key) >= 0); } public TValue this[TKey key] { get { int index = this.FindEntry(key); if (index >= 0) { return this.entries[index].value; } ThrowHelper.ThrowKeyNotFoundException(); return default(TValue); } set { this.Insert(key, value, false); } } } |
FindEntry()被調用了2次!這就導致速度變慢了1倍!
解決方案:估計微軟自己也發現 了,所以在2.0里面封裝了一個新的method:
| public bool TryGetValue(TKey key, out TValue value) { int index = this.FindEntry(key); if (index >= 0) { value = this.entries[index].value; return true; } value = default(TValue); return false; } |
于是,上面的代碼就可以改寫成:
| Dictionary<Key, Value> dict = new Dictionary<Key, Value>(); ........... Value value; if (dict.TryGetValue(key, out value)) { value....... } |
使用TryGetValue,FindEntry 只調用了一次,同時判斷了有沒有也得到了值。 在程序中常會有這樣的代碼。
多了可能會影響效率。
| Dictionary<Key, Value> dict = new Dictionary<Key, Value>(); ........... if (dict.ContainsKey(key)) { Value value = dict[key]; } |
看來沒有什么問題。
但是在實際項目中,這種代碼一般會寫在底層的class中。
它被調用的次數 相當大時,需要優化。
MS.Net2.0如何實現:
| public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback { public bool ContainsKey(TKey key) { return (this.FindEntry(key) >= 0); } public TValue this[TKey key] { get { int index = this.FindEntry(key); if (index >= 0) { return this.entries[index].value; } ThrowHelper.ThrowKeyNotFoundException(); return default(TValue); } set { this.Insert(key, value, false); } } } |
FindEntry()被調用了2次!這就導致速度變慢了1倍!
解決方案:估計微軟自己也發現 了,所以在2.0里面封裝了一個新的method:
| public bool TryGetValue(TKey key, out TValue value) { int index = this.FindEntry(key); if (index >= 0) { value = this.entries[index].value; return true; } value = default(TValue); return false; } |
于是,上面的代碼就可以改寫成:
| Dictionary<Key, Value> dict = new Dictionary<Key, Value>(); ........... Value value; if (dict.TryGetValue(key, out value)) { value....... } |
使用TryGetValue,FindEntry 只調用了一次,同時判斷了有沒有也得到了值。
轉載于:https://www.cnblogs.com/tiwlin/archive/2010/11/13/1876438.html
總結
以上是生活随笔為你收集整理的Dictionary加速查询(TryGetValue)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 个人博客发展前景
- 下一篇: hdu 1257 最少拦截系统 (DP)