C# List vs IEnumerable performance question

后端 未结 4 2043
伪装坚强ぢ
伪装坚强ぢ 2021-02-12 14:55

Hi suppose these 2 methods:

private List GetProviderForType(Type type)
        {
            List returnValue = new         


        
相关标签:
4条回答
  • 2021-02-12 15:12

    The Main Difference Between IEnumerable and IList:

    IEnumerable: Implements MoveNext,Reset,Get Current Methods and Returns a Type of IEnumerator to Iterate Through Records.

    IList : Exposes the IEnumerable Interface as well as it is also a collection of non-generic objects which can accessed through index so IEnumerable+ICollection(Manipulation of data) and add,remove,Insert(at specific Index) are the useful methods implemented by IList.

    After looking at your Code In My Opinion IEnumerable is more efficient but returning list is also useful if you want to do some manipulation with data and if you just want to Iterate through data then IEnumerable is preferable.

    0 讨论(0)
  • 2021-02-12 15:14

    In this particular case, using the IEnumerable<T> form will be more efficient, because you only need to know the count. There's no point in storing the data, resizing buffers etc if you don't need to.

    If you needed to use the results again for any reason, the List<T> form would be more efficient.

    Note that both the Count() extension method and the Count property will be efficient for List<T> as the implementation of Count() checks to see if the target sequence implements ICollection<T> and uses the Count property if so.

    Another option which should be even more efficient (though only just) would be to call the overload of Count which takes a delegate:

    private int GetProviderCount(Type type)
    {
      return _objectProviders.Count(provider =>
          (provider.Key.IsAssignableFrom(type) 
           || type.IsAssignableFrom(provider.Key))
          && provider.Value.SupportsType(type));
    }
    

    That will avoid the extra level of indirections incurred by the Where and Select clauses.

    (As Marc says, for small amounts of data the performance differences will probably be negligible anyway.)

    0 讨论(0)
  • 2021-02-12 15:17

    The precise answer to questions like this can vary depending on a lot of factors, and may change further as the CLR evolves. The only way to be sure is to measure it - and bear in mind that if the difference is small compared to the operation this will appear in, then you should pick the most readable, maintainable way of writing it.

    And on that note, you might also want to try:

    private IEnumerable<IObjectProvider> GetProviderForType1(Type type)
    {
        return _objectProviders.Where(provider => 
                      provider.Key.IsAssignableFrom(type) ||
                      type.IsAssignableFrom(provider.Key)) &&
                      provider.Value.SupportsType(type))
                               .Select(p => p.Value);
    }
    

    You can also give yourself a lot of flexibility by returning IEnumerable<T> and then using the ToList extension method if you want to "snapshot" the results into a list. This will avoid repeated evaluation of the code to generate the list, if you need to examine it multiple times.

    0 讨论(0)
  • 2021-02-12 15:32

    An important part of this question is "how big is the data"? How many rows...

    For small amounts of data, list is fine - it will take negligible time to allocate a big enough list, and it won't resize many times (none, if you can tell it how big to be in advance).

    However, this doesn't scale to huge data volumes; it seems unlikely that your provider supports thousands of interfaces, so I wouldn't say it is necessary to go to this model - but it won't hurt hugely.

    Of course, you can use LINQ, too:

    return from provider in _objectProviders
           where provider.Key.IsAssignableFrom(type) ...
           select provider.Value;
    

    This is also the deferred yield approach under the covers...

    0 讨论(0)
提交回复
热议问题