plz help optimize the small code snippet, VS2010 profiling chart provided Profiling shows MoveNext takes most time, which I guess is true as it is lazy. But I don't know in what direction I could improve the code. (Open pic in new tab would allow u see bigger chart.)



    IEnumerable&lt;string&gt; MixedBlock(List&lt;string&gt; reelLayout, string thisBlock, int length)
    {
        List&lt;string&gt; recentBlock = RecentBlock(length - 1, reelLayout);
        var upper = Enumerable.Range(1, length - 1);
        var lower = Enumerable.Range(1, totalWeight[thisBlock].Num);
        var query = from x in upper
                    from y in lower
                    where x + y == length
                    select Tuple.Create(x, y);
        foreach (var t in query)
        {
            string upperBlock = string.Join(",", recentBlock.Skip(length - 1 - t.Item1));
            string lowerBlock = string.Join(",", thisBlock.Split(',').Take(t.Item2));
            yield return upperBlock + "," + lowerBlock;
        }

    }
You're doing way more iterations than are necessary.. you don't need 2 enumerables of ints just to do this, that's a lot more heap references when all your need are 2 integers right on the stack, and a nested for loop:

int upperLimit = length - 1;
int lowerLimit = totalWeight[thisBlock].Num;
List&lt;string&gt; recentBlock = RecentBlock(length - 1, reelLayout);
List&lt;string&gt; stringResults = new List&lt;string&gt;();

for(int upper = 1; upper &lt; upperLimit; upper++)
{
  for(int lower = 1; lower &lt; lowerLimit; lower++)
  {
    if (upper + lower = length)
    {
      stringResults.Add(
        string.Join(",", recentBlock.Skip(length - 1 - upper)) + "," +
        string.Join(",", splits.Take(lower)));
    }
  }
}

return stringResults;


also instead of doing the stringResults.Add you could just yield return the string right there if you wanted to allow delayed execution.

Remember, just because we now have tuples and all the happy linqiness, they can be used to simplify complex things, though sometimes they just make operations more complex. If performance is important, study up on what goes on underneath the linq before throwing it around too much.