简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM