Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the performance hit of List.OfType<> where the entire list is that type?

I have an architecture where we are passing our data nodes as IEnumerable<BaseNode>. It all works great, but in each subclass we want to store these as List<AnotherNode> as everything in that class creates and uses AnotherNode objects (we have about 15 different subclasses).

The one place using the more strongly typed list doesn't work is the root classes method that returns a type IEnumerable<BaseNode> and with the covariance limitations in .net 3.5, that can't be returned. (We have to stay on .net 3.5 for now.)

But if I have List<AnotherNode> data; and return data.OfType<BaseNode>(); - that works fine. So here's my question.

As all of data is of type BaseNode - what's the performance hit of this call? Because the alternative is I have to cast in places which has a small performance hit - but it's also a situation where we give up everything knowing it's type.

like image 652
David Thielen Avatar asked Aug 25 '11 18:08

David Thielen


People also ask

What's new on the Hit List?

The Hit List returns for a fifth series and this time, it’s going to blow your mind…and your speakers! Marvin and Rochelle Humes are back hosting this exciting and addictive music quiz, jam-packed with big hits and more pop facts than ever before.

How to use oftype operator in LINQ?

OfType Operator 1 In LINQ, you are allowed to use multiple OfType in the single query. 2 It does not support query syntax in C 3 or VB.Net language, but you can use it with syntax as shown in the below example. 4 It supports method syntax in C 5 or in VB. It available in both the Queryable and Enumerable class. As shown in the below... More ...

What is the performance of a list vs array?

I think the performance will be quite similar. The overhead that is involved when using a List vs an Array is, IMHO when you add items to the list, and when the list has to increase the size of the array that it's using internally, when the capacity of the array is reached.

How do I perform basic operations on lists?

To perform basic operations on lists, use the functions in the List module. You can define a list by explicitly listing out the elements, separated by semicolons and enclosed in square brackets, as shown in the following line of code. You can also put line breaks between elements, in which case the semicolons are optional.


2 Answers

Two minor things:

  1. There is a small, but measurable overhead associated with yielding each item in the enumerator. If you need to care about this because you're in a very tight inner loop, you're actually better off iterating with a for loop on the list directly. Most likely this doesn't matter.

  2. Because the result is IEnumerable<BaseNode> and has already been filtered through a yielding enumeration function, subsequent calls to methods like Count() or ElementAt() will not take advantage of optimizations in the LINQ implementation for Lists. This is also unlikely to be a problem unless you make frequent use of these extension methods and have a very large number of elements.

like image 186
Dan Bryant Avatar answered Oct 19 '22 07:10

Dan Bryant


Have you seen the Cast<T>() Linq operator? It should be more performant than OfType<T>().

Basically there is a condition that is run with OfType<T>()

if (item is T) {
    yield return (T)item;
}

Contrast that with what Cast<T>() does:

yield return (T)item;
like image 22
Daniel A. White Avatar answered Oct 19 '22 07:10

Daniel A. White