Reputation: 1445
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();
}
}
}
}
...
Upvotes: 0
Views: 566
Reputation: 5005
In the first I use an If-Else statement and in the second I omit the Else, because the If case is quite rare.
That just means your second program does not perform exactly as the first (unless you can prove that the else
will never be required...). So it's not a problem of performance, your second program is wrong (assuming your first program is correct and the else is required).
Fast programs that don't output the correct results are kind of a bad thing.
In this particular case a good compiler will optimize the code to something like alzaimar's answer. You should write it like that too, though, for readability.
In the general case, an else may(will) include a performance penalty through something called branch prediction failure
. A modern CPU will "guess" whether the program flow will go through the if
or the else
and execute that branch. If it later discovers it made the wrong guess it has to backtrack and go through the correct branch.
If this actually becomes an issue, you should make sure that the order in which the if
and else
branches are used follows a simple pattern.
See this question and its answers for more details.
A second cause of slow performance would come from not taking advantage of something called data locality or locality of reference. This means that you should use data that is close together (such as myArray[100] and myArray[101]) close together.
In your particular case, don't change the order in which you index your array.
Write a correct program first. Then optimize it where it hurts. That is, where a profiler shows your program spends too much time. It's no use optimizing things that don't matter.
Upvotes: 2
Reputation: 100527
Directly assigning should be perfectly fine in your sample:
BooleanAnswerArray[index][x][y] = SomeFunction(index,x,y);
Side note: it may be good idea to experiment with caching array access - should be able to cache var row = BooleanAnswerArray[index][x]
so you avoid extra indexing in innermost loop.
On updated question with if/else:
First of - this is performance question so one must measure different options and see if code meets the goals. Stopwatch
class is usually enough for such localized performance comparisons, otherwise profiler may be needed.
Speculations
if
will cause exactly the same impact whether it has one or both branches especially in presence of any other non-trivial code like non-inlined function call. Upvotes: 5
Reputation: 40659
Your method 1 does the 3-level index calculation only once instead of twice.
Whether the compiler could optimize that is iffy.
But of course, everything depends on the relative time used by SomeFunction
.
Upvotes: 0
Reputation: 4622
What prevents you from simplifying the code as follows:
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] = SomeFunction(index,x,y);
Sorry for kicking the braces. I don't like them too much ;-)
Upvotes: 1
Reputation: 12837
the first one should be marginally faster. but if I were you I would care more about readable code than well performing code (that is till you start doing something millions of times)... just my 2c
Upvotes: 0