Wednesday 15 July 2015

C# linq order by and other statements with foreach, is there a performance difference? -



C# linq order by and other statements with foreach, is there a performance difference? -

i'm pogramming lot entity framework, collections , there usting where-conditions , order actions. i've been asking myself while, never able figure out.

let i've got follwing 2 pieces of code;

example 1:

// unsorted string array. string[] letters = { "d", "c", "a", "b" }; // utilize linq query syntax sort array alphabetically. var sorted = letter in letters orderby letter select letter; // loop foreach keyword. foreach (string value in sorted) { console.writeline(value); }

example 2:

// unsorted string array. string[] letters = { "d", "c", "a", "b" }; // loop foreach keyword. foreach (string val in letters.orderby(l => l)) { console.writeline(val) }

the first illustration first order on resultset , iterates on collection sec 1 has order on moment we're going iterate on it. real question i've been wondering while where.. difference in performance (if there any)? , 1 of 2 methods improve other one? same conditons (simple , complex ones joins), there notable difference?

there no difference between two..

code -

static void main(string[] args) { string[] letters = { "d", "c", "a", "b" }; // utilize linq query syntax sort array alphabetically. var sorted = letter in letters orderby letter select letter; // loop foreach keyword. foreach (string value in sorted) { console.writeline(value); } foreach (string val in letters.orderby(letter => letter)) { console.writeline(val); } }

generated code -

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) { homecoming letter; } [compilergenerated] private static string \u003cmain\u003eb__1(string letter) { homecoming letter; }

edit: here interesting variation tempted try.. in above case, query expression, compiler smart plenty optimize select away.. if, on sec variation, tack on explicit select. kind of not surprising, still, compiler doesn't optimize away explicit .select (vs explicit select in query look - requirement compiler).

code -

foreach (string val in letters.orderby(letter => letter).select(letter => letter)) { console.writeline(val); }

generated code -

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);

c# performance linq query-performance

No comments:

Post a Comment