I know that it is impossible to use return and yield return in the same method.
This is the code I would like to optimize:
public IEnumerable<TItem> GetItems(int data) { if (this.isSingleSet) { return this.singleSet; // is IEnumerable per-se } else { int index = this.GetSet(data); foreach(TKey key in this.keySets[index]) { yield return this.items[key]; } } }
Important: I know that this code does not compile . This is the code I need to optimize.
There are two ways that I know for this method to work:
convert yield return part:
... else { int index = this.GetSet(data); return this.keySets[index].Select(key => this.items[key]); }
convert return part:
if (this.isSingleSet) { foreach(TItem item in this.singleSet) { yield return item; } } else ...
But between them there is a big difference in speed. Using only return (in other words, using Select() ) is much slower (e.g., 6 times slower) yield return conversion.
Question
Is there another way that comes to your mind how to write this method? Do you have any other suggestions that will be useful for performance differences?
Additional Information
I measured the speed of two methods using a stopwatch around the for loop.
Stopwatch s = new Stopwatch(); s.Start(); for(int i = 0; i < 1000000; i++) { GetItems(GetRandomData()).ToList(); } s.Stop(); Console.WriteLine(s.ElapsedMilliseconds);
Each of these loops was performed in separate processes, so there could be no impact on garbage collection performance or anything else.
- I ran the program with one version of the method, then
- Closed
- I rewrote the method and run it again.
This happened several times to see a reliable difference in performance ...
source share