In something as simple as
int sq(int x) { int y = x*x; return y; }
versus
int sq(int x) { return (x*x); }
the former function requires an extra IL step.
EDIT: IL code from the first example, using the current C# compiler from VS 2013 Update 4 (.NET Framework 4.5.1), code from the optimized release version:
IL_0000: ldarg.0
IL_0001: ldarg.0
IL_0002: mul
IL_0003: stloc.0 // These two instructions
IL_0004: ldloc.0 // seem to be unneccessary
IL_0005: ret
and from the second example:
IL_0000: ldarg.0
IL_0001: ldarg.0
IL_0002: mul
IL_0003: ret
Is that IL extra step indicative of data being copied in memory? Is this difference still present when using something seemingly better optimized? (C?)
If yes to first or both: can I use the latter, especially when the returned data takes a lot of space, and presumably a lot of time to be moved in memory? Or should I favor the former, because it is more readable? LINQ makes it easy to have whole functions inside a long return() line, but my example faces the same choice were it C. Speed or readability?
In general, would you put the last bit of maths between the parentheses in the return line?
nop
s in your assembly code are a clear indication that JIT optimizations are disabled. Using the default settings VS disables JIT optimizations if you run in the debugger. – CodesInChaos Dec 21 '14 at 15:04return(somelist.Where(..).Select(..).Union(anotherlist).Where(..)))
, and so on. Or more explicitlynewlist = somelist.Where(..).Select(..); secondnewlist = .. ; newlist.UnionWith(..); return(newlist)
. Should I put loads of things into the return line, with the benefit that I am getting rid of some data duplication steps? I am not a compiler designer, and so would have hoped that both compile to the same EXE - and would have gone for readability. But then LinqPad showed me different IL codes. So now there is a dillema: speed or readability? – nvja Dec 21 '14 at 19:22