Frage

Questions:

Is it generally at all better to use if statements or equations when dealing with boolean values? Please address the following questions, keeping in mind that your answer should apply to most, if not all code:

  1. Which is faster, and why? Or is the difference too small to be significant?
  2. Is one of them more dynamic? Is there a reason one of them may fall short to the other in certain scenarios?
  3. Which is the better/preferred code, or is the practice of either entirely opinion based?

(I am referring to C++, but as this question is semi-generic, references or comparisons to other languages would also be appreciated.)

Example (in C++):

Given the following broad scenario,

int n = (anything);
bool x_not_y = (declared as random boolean value);

And the goal being to make int x = n if x_not_y is true and int y = n instead on the contrary, is it better to use an if/else statement:

if (x_not_y)
    x = 5;
else
    y = 5;

(shorthand below)

x_not_y?x = 5:y = 5;

or to use an equation?

x = (x * (int)!x_not_y) + (5 * (int)x_not_y);
y = (y * (int)x_not_y) + (5 * (int)!x_not_y);
War es hilfreich?

Lösung

Which is faster, and why? Or is the difference too small to be significant?

I doubt such code would make a difference to modern optimizers, but if you want to know, we first would have to ask, What platform, what compiler?, and then we would need to measure.

Is one of them is more dynamic? Is there a reason one of them may fall short to the other in certain scenarios?

As I happen to believe that your compiler's optimizer can deal with all of them equally, I would suggest to write code that's easy to understand and maintain and deal with optimizations only after measuring reveals that this is a hotspot.

Which is the better/preferred code, or is the practice of either entirely opinion based?

Over time, I have found that I am leaning more and more towards the functional paradigm. I like expressing branches through the code's structure better than through variable values, I prefer to constify as much as possible (even locale variables).

I have especially learned to dislike code like this:

int x;
if( some_condition )
    x = 5;
else
    x = 42;

In order to understand what code like this does, one has to track the values of each variable and follow each statement. Also, x cannot be const here, which I consider a string disadvantage.

I strongly prefer

const int x = calc_x(some_condition); 

which, in cases as easy as this example, can be written as

const int x = some_condition ? 5 : 42;

Now x can be const (allowing the compiler to prevent me from unintentionally doing silly things), the piece of code implementing the algorithm that finds out what x ought to be has a name (calc_x()), and rather than having the algorithm inline, now only that name is in the code.

Of course, this code is so simple that it doesn't really need the benefits I am listing, but when you deal with more complex real-world code, I have found that, in the long run, a more functional approach is be better.

Andere Tipps

Your two examples do two different things:

The IF statement changes x or y, leaving the other unchanged.

Your equation changes both x & y, setting one to the value 5 and the other to 0.

That said, the if statement may suffer a branch misprediction penalty, where the second form would not (at the cost of being hard to read).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top