I've been through a few questions over the network about this subject but I didn't find any answer for my question, or it's for another language or it doesn't answer totally (dead code is not useless code) so here's my question:
Is (explicit or not) useless code ignored by the compiler?
For example, in this code:
double[] TestRunTime = SomeFunctionThatReturnDoubles;
// A bit of code skipped
int i = 0;
for (int j = 0; j < TestRunTime.Length; j++)
{
}
double prevSpec_OilCons = 0;
will the for loop be removed?
The background is that I maintain a lot of code (that I didn't write) and I was wondering if useless code should be a target or if I could let the compiler take care of that.
I've made a little form to test it according to a few answerers ideas about using
long.MaxValue
, here's my reference code:and here's the code with kinda useless code:
You'll remark that I used
int.MaxValue
instead oflong.MaxValue
, I didn't want to spend theyearday on this one.As you can see:
The code isn't optimized. Hang on a bit, I'll try with some
int[]
to testint[].Lenght
:And here's the results:
So even with arrays, it doesn't get optimized at all.