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?
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
You should take a look on this:
LINQ and Deferred Execution
精彩评论