C # linq order and other statements with foreach, is there any difference in performance?

I draw a lot with an entity framework, collections, and where conditions and procedures are also used. I asked myself about this for a while, but could not understand.

Say I have two pieces of code:

EXAMPLE 1:

// An unsorted string array.
string[] letters = { "d", "c", "a", "b" };
// Use LINQ query syntax to sort the array alphabetically.
var sorted = from letter in letters
         orderby letter
         select letter;

// Loop with the foreach keyword.
foreach (string value in sorted)
{
    Console.WriteLine(value);
}

EXAMPLE 2:

// An unsorted string array.
string[] letters = { "d", "c", "a", "b" };

// Loop with the foreach keyword.
foreach (string val in letters.OrderBy(l => l))
{
    console.writeline(val)
}

The first example first executes the order on the result set, and then iterates over the collection in which the second has order at the moment when we iterate over it. Now the real question, about which I have long wondered where. What is the difference in performance (if any)? And is one of the two methods better than the other? Same with conditons terms (simple and complex with unions), is there any noticeable difference?

+4
3

.

-

static void Main(string[] args)
{
    string[] letters = { "d", "c", "a", "b" };
    // Use LINQ query syntax to sort the array alphabetically.
    var sorted = from letter in letters
                    orderby letter
                    select letter;

    // Loop with the foreach keyword.
    foreach (string value in sorted)
    {
        Console.WriteLine(value);
    }

    foreach (string val in letters.OrderBy(letter => letter))
    {
        Console.WriteLine(val);
    }
}

-

private static void Main(string[] args)
{
  string[] strArray1 = new string[4]
  {
    "d",
    "c",
    "a",
    "b"
  };
  string[] strArray2 = strArray1;
  if (Program.CS\u0024\u003C\u003E9__CachedAnonymousMethodDelegate2 == null)
  {
    // ISSUE: method pointer
    Program.CS\u0024\u003C\u003E9__CachedAnonymousMethodDelegate2 = new Func<string, string>((object) null, __methodptr(\u003CMain\u003Eb__0));
  }
  Func<string, string> keySelector1 = Program.CS\u0024\u003C\u003E9__CachedAnonymousMethodDelegate2;
  foreach (string str in (IEnumerable<string>) Enumerable.OrderBy<string, string>((IEnumerable<string>) strArray2, keySelector1))
    Console.WriteLine(str);
  string[] strArray3 = strArray1;
  if (Program.CS\u0024\u003C\u003E9__CachedAnonymousMethodDelegate3 == null)
  {
    // ISSUE: method pointer
    Program.CS\u0024\u003C\u003E9__CachedAnonymousMethodDelegate3 = new Func<string, string>((object) null, __methodptr(\u003CMain\u003Eb__1));
  }
  Func<string, string> keySelector2 = Program.CS\u0024\u003C\u003E9__CachedAnonymousMethodDelegate3;
  foreach (string str in (IEnumerable<string>) Enumerable.OrderBy<string, string>((IEnumerable<string>) strArray3, keySelector2))
    Console.WriteLine(str);
}

[CompilerGenerated]
private static string \u003CMain\u003Eb__0(string letter)
{
  return letter;
}

[CompilerGenerated]
private static string \u003CMain\u003Eb__1(string letter)
{
  return letter;
}

EDIT: , . , Select. , Select. , , , .Select (vs explicit Select - ).

-

    foreach (string val in letters.OrderBy(letter => letter).Select(letter => letter))
    {
        Console.WriteLine(val);
    }

-

  string[] strArray4 = strArray1;
  if (Program.CS\u0024\u003C\u003E9__CachedAnonymousMethodDelegate6 == null)
  {
    // ISSUE: method pointer
    Program.CS\u0024\u003C\u003E9__CachedAnonymousMethodDelegate6 = new Func<string, string>((object) null, __methodptr(\u003CMain\u003Eb__2));
  }
  Func<string, string> keySelector3 = Program.CS\u0024\u003C\u003E9__CachedAnonymousMethodDelegate6;
  IOrderedEnumerable<string> orderedEnumerable = Enumerable.OrderBy<string, string>((IEnumerable<string>) strArray4, keySelector3);
  if (Program.CS\u0024\u003C\u003E9__CachedAnonymousMethodDelegate7 == null)
  {
    // ISSUE: method pointer
    Program.CS\u0024\u003C\u003E9__CachedAnonymousMethodDelegate7 = new Func<string, string>((object) null, __methodptr(\u003CMain\u003Eb__3));
  }
  Func<string, string> selector = Program.CS\u0024\u003C\u003E9__CachedAnonymousMethodDelegate7;
  foreach (string str in Enumerable.Select<string, string>((IEnumerable<string>) orderedEnumerable, selector))
    Console.WriteLine(str);
+3

... = letters.OrderBy(letter => letter).Select(letter => letter);

... in letter.OrderBy(l => l)) {

, .Select(...), , . # , IL, .

, . .OrderBy(...), linq-, . , .OrderBy(...) , , . , foreach (... in <collection>) , .

: , - . , .

+2

The only difference I see is that in the first code Iteratorcreated (not executed) before foreach, and you save it in a local variable. In the second code foreach, the method is called GetEnumaratoron the iterator, and not on the local variable. But of course, this will not matter in terms of performance. In addition, I would prefer the second because of readability.

0
source

All Articles