Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# LINQ optimization

What would be the proper way of optimizing the following kind of statements:

IEnumerable<T> sequence = BuildSequence();
// 'BuildSequence' makes some tough actions and uses 'yield return'
// to form the resulting sequence.

Now, if I'm willing to take only some of the first elements, I could use something like:

sequence.Take(5);

And so, if my sequence from BuildSequence actually contains several thousands of elements, I obviously don't want all of them to be constructed, because I would only need 5 of them.

Does LINQ optimize this kind of operations or I would have to invent something myself?

like image 409
Yippie-Ki-Yay Avatar asked Apr 05 '11 13:04

Yippie-Ki-Yay


1 Answers

The iterator block (yield return) handles this for you; an iterator block is a streaming API; work only happens during each MoveNext() call on the iterator (or on Dispose()). Because Take() also doesn't read the entire stream, this behaviour is preserved.

Note, however, that some operations need to buffer the data locally - GroupBy and OrderBy most notably; but as long as you use non-buffering operations, you are fine.

For example:

static IEnumerable<int> ReadInts() {
    var rand = new Random();
    while(true) yield return rand.Next();
}
...
int count = ReadInts().Take(10).Count(); // 10 - it doesn't loop forever
like image 81
Marc Gravell Avatar answered Oct 20 '22 04:10

Marc Gravell