zoukankan      html  css  js  c++  java
  • LINQ Dictionary加速查询(TryGetValue)

    在程序中常会有这样的代码。
    多了可能会影响效率。

    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 只调用了一次,同时判断了有没有也得到了值。

  • 相关阅读:
    利用kettle中的JS来完成ETL数据校验
    spring cloud学习地址
    centos7 卸载 gitlab
    为什么WEB-INF外的jsp无法根据cookie享受国际化
    改变maven父子项目视图为树状
    maven profiles、filters、resources学习笔记 及 常用 plugin demo
    Tomcat 签名认证配置简例
    CentOS 开机启动
    Tomcat 关闭时报错
    比较全的log4j示例
  • 原文地址:https://www.cnblogs.com/chengulv/p/3085733.html
Copyright © 2011-2022 走看看