Reputation: 48793
Lets assume I have this code:
if (a == 'something') {
doA();
} else {
doB();
}
My main concern is related to performance. If in most (almost 99%) cases a
is not equal to 'something'
do I need to invert conditions and blocks?
if (a != 'something') {
doB();
} else {
doA();
}
Upvotes: 3
Views: 100
Reputation:
The engine is going to do a better job of optimizing this than you can. I find it a bit difficult to believe that this is really where the bottleneck in your code is. Is that what the profiler is saying?
There is a notion of "branch prediction", where the engine decides which alternative is more likely, and organizes the machine code so that the code falls through in the more common case and takes the JMP only in the less common case.
However, even in the absence of such optimization, assuming the code is optimized by the engine, the difference in the two cases is likely to be at the level of several dozen nanoseconds, and even if the code is not optimized for whatever reason, and is being "interpreted", the difference will be measured in microseconds. If each page view involves ten such conditional statements, the total extra load per day on your server might be a few dozen seconds of CPU time.
When considering this problem, or doing benchmarks, note that there could be a difference depending on differences in time required to evaluate the condition. For instance, !b
will take longer to evaluate than b
. If you can find a way to optimize the comparison--since string comparisons presumably are more expensive than eg numeric comparisons--that might buy you something.
Upvotes: 2
Reputation: 45659
Most answers have the right conclusion with wrong reasoning.
if (condition) {
// do X
} else {
// do something that takes exactly the same amount of time as X
}
The performance of a pass where condition
is true can absolutely be different from the performance of a pass where condition
is false. How and why depends on the mechanism for executing instructions, all the way down to the microprocessor - which makes an accurate discussion very difficult when we're talking about a language that's interpreted in a web browser.
Luckily it does not matter. Compilers, runtime engines, and even the microprocessor itself handle that level of optimization precisely because (a) it's not humanly possible to do it by hand, and (b) in the long run attempts to do so are as likely as not to backfire. That - and not how often the code runs - is what makes it a micro optimization not worth spending time on.
Upvotes: 0
Reputation: 9858
Below is how you could speed-test this. With a quick test using JSFiddle and Chrome, I sometimes but not consistently found inverting the conditions saved some time (I initially got times of 11.6s vs. 6.7s), but it might well be platform-dependent and/or depend on the doSomething
functions.
If this is client-side code, then this is unlikely to have a noticeable effect on performance unless the same decision is run thousands or millions of times for each user.
function doSomething() {
document.getElementById('test').innerHTML = parseInt(Math.random()*10);
}
function doSomethingElse() {
document.getElementById('test2').innerHTML = parseInt(Math.random() * 10);
}
function normal(x) {
if (x == true) {
doSomething();
} else {
doSomethingElse();
}
}
function inverted(x) {
if (x != true) {
doSomethingElse();
} else {
doSomething();
}
}
function callFunc(func, repetitions) {
var p;
for (var i = 0; i < repetitions; i++) {
p = Math.random() > .99;
func(p);
}
}
var reps = 1000000;
console.time('normal');
callFunc(normal, reps);
console.timeEnd('normal');
console.time('inverted');
callFunc(inverted, reps);
console.timeEnd('inverted');
Upvotes: 0
Reputation: 12439
You have a condition to decide with only 2 possible outcomes true
or false
. You can't really do anything with this scenario.
If you would have some what like the following scenario:
if (a == 'something') {
doA();
} else if (a == 'something else') {
doB();
} else if (a == 'something else else') {
doC();
} else {
doD();
}
and you would say that condition a == 'something else else'
gets true most of the times, then you should definitely move it to the top and this micro optimization
makes sense.
But in existing example you have the minimal required code to be executed.
Upvotes: 2
Reputation: 9786
There is no improvement as you would in any case do a check condition, and take one of the respective branches.
One (unrelated) advice if you have several if
conditions is to write them like this:
if ('something' == a) {
doA();
} else {
doB();
}
This because it is easy to put a = 'something'
equal and it will be taken as an assignment.
Upvotes: 0
Reputation: 1523
No, you don't need that. An if is only a conditional statement, anyway the code will need to chose if it is true or false;
Upvotes: 0