I'm writing a Monte Carlo simulation in c# and I'm trying to make sure that I write code which is as efficient as possible---I'm running billions of loops and things are getting slow. I have a question about using Else
statements inside loops.
My question is this: Is there any difference in performance between these two methods? In the first I use an If-Else statement and in the second I omit the Else, because the If case is quite rare.
EDIT: Lets assume I need to do more than just assign true/false when the condition is met, so that direct assignment isn't the only thing that needs to be done. Does the if-Else way perform just as quickly?
//METHOD 1
...
for (int index = 0; index < 6; index++)
{
for (int x = 0; x < 50; x++)
{
for (int y = 0; y < 50; y++)
{
bool ThingWhichIsVeryRarelyTrue = SomeFunction(index,x,y);
if (ThingWhichIsVeryRarelyTrue)
{
BooleanAnswerArray[index][x][y] = true;
DoSomeOtherStuff();
}
else
{
BooleanAnswerArray[index][x][y] = false;
}
}
}
}
...
//METHOD 2
for (int index = 0; index < 6; index++)
{
for (int x = 0; x < 50; x++)
{
for (int y = 0; y < 50; y++)
{
BooleanAnswerArray[index][x][y] = false;
bool ThingWhichIsVeryRarelyTrue = SomeFunction(index,x,y);
if (ThingWhichIsVeryRarelyTrue)
{
BooleanAnswerArray[index][x][y] = true;
DoSomeOtherStuff();
}
}
}
}
...