Ok, to answer your question the way you want it because you're being difficult. If you want a different answer, I highly suggest that you actually ask the question you want answered.
I am compiling this code, first with only region 1 and then second with only region 2:
int var_a = 0;
//...
//Some code that fetches var_a from db if db field is not null
//...
// region 1
if(var_a != null && var_a > 0) var_a = -1;
//region 2
if(var_a != null){
if (var_a > 0) var_a = -1;
}
If I extract the IL code for region 1, I get this:
IL_0015: nop
IL_0016: ldc.i4.0
IL_0017: stloc.0
IL_0018: ldloc.0
IL_0019: ldc.i4.0
IL_001a: cgt
IL_001c: ldc.i4.0
IL_001d: ceq
IL_001f: stloc.1
IL_0020: ldloc.1
IL_0021: brtrue.s IL_0025
IL_0023: ldc.i4.m1
IL_0024: stloc.0
IL_0025: ldloc.0
And for region 2, I get this:
IL_0015: nop
IL_0016: ldc.i4.0
IL_0017: stloc.0
IL_0018: ldc.i4.1
IL_0019: ldc.i4.0
IL_001a: ceq
IL_001c: stloc.1
IL_001d: nop
IL_001e: ldloc.0
IL_001f: ldc.i4.0
IL_0020: cgt
IL_0022: ldc.i4.0
IL_0023: ceq
IL_0025: stloc.1
IL_0026: ldloc.1
IL_0027: brtrue.s IL_002b
IL_0029: ldc.i4.m1
IL_002a: stloc.0
IL_002b: nop
IL_002c: ldloc.0
So, yes, there is a slightly difference. JetBrains DotPeek shows a difference.
Region 1:
if (num > 0)
num = -1;
Region 2:
int num = 0;
bool flag = 1 == 0;
if (num > 0)
num = -1;
Whereas JustDecompile cleans things up a bit and shows the same IL->C# conversion for both:
if (var_a > 0)
{
var_a = -1;
}
Since you care so much about efficiency, I've written a quick bit of code to try and benchmark the difference:
Random rn = new Random();
List<int> l = new List<int>();
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
for (int j = 1; j <= 20; ++j)
{
l.Clear();
sw.Start();
if (j % 2 == 0)
{
Console.Write("A: ");
for (int i = 0; i < 100000000; ++i)
{
int var_a = rn.Next(1, 10000) * (rn.NextDouble() <= 0.5 ? -1 : 1);
if (var_a != null)
if (var_a > 0) var_a *= -1;
l.Add(var_a);
}
}
else
{
Console.Write("B: ");
for (int i = 0; i < 100000000; ++i)
{
int var_a = rn.Next(1, 10000) * (rn.NextDouble() <= 0.5 ? -1 : 1);
if (var_a != null && var_a > 0) var_a *= -1;
l.Add(var_a);
}
}
sw.Stop();
Console.WriteLine(sw.Elapsed.TotalMilliseconds);
sw.Reset();
}
Values for A:
2918.6503
2910.8609
2916.2404
2909.5394
2914.0309
2961.0775
2948.4139
2957.1939
2962.1737
Values for B:
3170.8064
2891.6971
2924.8533
2890.6248
2885.1991
2890.6321
2887.0145
2935.6778
2909.035
The average for the entire 100,000,000 execution cycle for A vs B is 2933 ms
vs 2932 ms
respectively.
Per execution of the internal block, that's 2.9331 * 10^-5
vs 2.9317 * 10^-5
.
Now we've got down to this, I have to ask why you would be writing a program in as high a level language as C# when you care about something that makes 0.000000014045333333 ms
difference between one way and the other. Perhaps you should try something more low level like Assembly? All in all, this discrepancy could still come down to activity of the CPU during that operation doing other things for Windows.
I hope this answer goes into the depth you've come to expect from StackOverflow.