Ternary vs if statement: compiler optimization

It:

int val; // ... val = (val != 0) ? otherVal : 0; 

less effective than this:

 int val; //... if (val != 0) val = otherVal; 

?

Can the compiler optimize a three-dimensional operator? The goal is clear, is there a way that one could actually write 0 to memory? Maybe when the memory is mapped to a file?

Can it be assumed that this does not matter?

EDIT: point - set the variable to some value if one condition is met. No branching required. so I ask if the triple will be less efficient or optimized (with a mandatory child branch that needs to make a copy).

+4
source share
4 answers

Mats Peterssonโ€™s suggestion is usually the best โ€œWrite the most read option.โ€ However, if you are trying to write the optimal speed code, you need to know more information about your computer and processor. On some machines, the first one will run faster (highly pipelined processors: no branching, optimized ternary operator). Other machines will run faster with the second form (easier).

+3
source

Your compiler optimizes it. In the end, there is no difference in performance.

However, there is a big difference in readability. Sometimes a ternary operator can help remove many lines of code that do not add a lot of clarity.

In other cases, the if clearer and simpler.

Reducing the code to a ternary statement, but then adding a ton of comments to maintain clarity is counterproductive.

And by all gods of coding, please do not insert ternary statements.

+3
source

Is this basically a duplicate ternary operator ?: vs if ... else

For most compilers, the efficiency will be the same, and the compiler optimizes the three-dimensional operator in the same way as it optimizes the if / else operator. However, I prefer statements if they make the code much easier to read with a quick glance.

To answer other questions. I'm not sure what you mean, if you just set a single integer or variable to 0, then there is no faster way than setting it to zero, as you have above.

If you have an array of variables, you can use memset(ptr, 0, size*sizeof(TYPE)) , which would probably be the fastest if you had an array of variables that you wanted to set to zero. Or maybe std :: fill_n

I'm not sure what you are trying to achieve using the logic above, but that seems a little strange. There are ways to streamline the code, which will mean that you probably won't need the conditional condition at all, but it's hard to say for your situation without seeing a bit more code.

Honestly, if you do not perform this operation billions of times, this is probably a very preliminary optimization, and you should focus on readability.

+1
source

You can use a non-stationary ternary operator, sometimes called a beatselek (condition? True: false).

Don't worry about extra operations; they are nothing compared to the if branch.

bitselect implementation:

 inline static int bitselect(int condition, int truereturnvalue, int falsereturnvalue) { return (truereturnvalue & -condition) | (falsereturnvalue & ~(-condition)); //a when TRUE and b when FALSE } inline static float bitselect(int condition, float truereturnvalue, float falsereturnvalue) { //Reinterpret floats. Would work because it just a bit select, no matter the actual value int& at = reinterpret_cast<int&>(truereturnvalue); int& af = reinterpret_cast<int&>(falsereturnvalue); int res = (at & -condition) | (af & ~(-condition)); //a when TRUE and b when FALSE return reinterpret_cast<float&>(res); } 
+1
source

Source: https://habr.com/ru/post/1482204/


All Articles