Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# List<T> vs IEnumerable<T> performance question

Hi suppose these 2 methods:

private List<IObjectProvider> GetProviderForType(Type type)
        {
            List<IObjectProvider> returnValue = new List<IObjectProvider>();

            foreach (KeyValuePair<Type, IObjectProvider> provider in _objectProviders)
            {
                if ((provider.Key.IsAssignableFrom(type) ||
                    type.IsAssignableFrom(provider.Key)) &&
                    provider.Value.SupportsType(type))
                {
                    returnValue.Add(provider.Value);
                }
            }
            return returnValue;
        }

private IEnumerable<IObjectProvider> GetProviderForType1(Type type)
        {
            foreach (KeyValuePair<Type, IObjectProvider> provider in _objectProviders)
                if ((provider.Key.IsAssignableFrom(type) ||
                    type.IsAssignableFrom(provider.Key)) &&
                    provider.Value.SupportsType(type))

                    yield return provider.Value;              
        }

Which one is quicker? When I look at the first method, I see that the memory is allocated for List, what in my opinion it's not needed. The IEnumerable method seems to be quicker to me.

For instance, suppose you call

int a = GetProviderForType(myType).Count;
int b = GetProviderForType1(myType).Count();

Now, another issue is, is there a performance difference between these 2 above?

What do you think?

like image 417
theSpyCry Avatar asked Jul 31 '09 09:07

theSpyCry


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

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.)

like image 152
Jon Skeet Avatar answered Sep 21 '22 18:09

Jon Skeet


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...

like image 41
Marc Gravell Avatar answered Sep 22 '22 18:09

Marc Gravell