Is a function in an if statement efficient? (C ++)

I saw statements like this

if(SomeBoolReturningFunc()) { //do some stuff //do some more stuff } 

and I'm wondering if the function is effective in the if statement, or there are cases where it would be better to leave them separately, for example, this

 bool AwesomeResult = SomeBoolReturningFunc(); if(AwesomeResult) { //do some other, more important stuff } 

...

+4
source share
11 answers

I'm not sure what makes you think that assigning the result of an expression to a variable at first will be more efficient than evaluating the expression itself, but it will never matter, so choose an option that improves the readability of your code. If you really want to know, look at the output of your compiler and see if there is a difference. In the vast majority of systems, this is likely to result in an identical machine code.

+26
source

In any case, it does not matter. the main idea is that the result will be stored in a temporary variable, regardless of whether you name it or not. Readability is more important right now because computers are usually so fast that small settings don't matter.

+6
source

Of course, I saw if (f ()) {blah;} produces more efficient code than bool r = f (); if (r) {blah;}, but that was many years ago at 68000.

These days, I definitely choose a code that is easier to debug. Your inefficiency is much more likely for your algorithm compared to the code that your compiler generates.

+3
source

as others have said, it basically does not affect performance, as a rule, in C ++ most of the performance gains at the pure code level (as opposed to the algorithm) are performed in the loop unfolding mode.

also, avoiding branching at all, can give better performance if the condition is in a loop.

for example, using separate loops for each conditional case or using operators that essentially take the condition into account (possibly by multiplying the term by 0 if it is not needed)

and then you can get more by expanding this loop

Your code templates can help with this in a β€œsemi” clean way.

+2
source

There is also the possibility

 if (bool AwesomeResult = SomeBoolRetuningFunc()) { ... } 

:)

IMO, the kind of instructions you saw are more readable and fewer errors:

 bool result = Foo(); if (result) { //some stuff } bool other_result = Bar(); if (result) { //should be other_result? Hopefully caught by "unused variable" warning... //more stuff } 
+2
source

Both variants usually produce the same machine code and work in exactly the same way. There will rarely be a difference in performance, and even then it is unlikely to become a bottleneck (which translates to not worry before profiling).

A significant difference is debugging and readability. With a temporary variable it’s easier to debug. Without a variable, the code is shorter and perhaps more readable.

If you want to both easily debug and read easier, you'd better declare the variable as const :

 const bool AwesomeResult = SomeBoolReturningFunc(); if(AwesomeResult) { //do some other, more important stuff } 

it is so clear that a variable is never assigned again, and there is no other logic to its declaration.

+2
source

Discarding problems with ease or ease of debugging and until the return value of the function is used again in the if-block; It seems to me that assigning a return value to a variable only leads to the additional use of the = operator and the additional bool variable stored in the stack space - I can further assume that the additional variable in the stack space will lead to a delay in the additional access stack (not sure though) .

The fact is that these are really small problems, and as long as the compiler has an optimization flag, it should not cause inefficiencies. In another case, I would think that it would be an embedded system - and again, how much damage can one 8-bit variable cause? (I have absolutely no knowledge of embedded systems, so maybe someone can do this?)

+1
source

It seems to me that one statement per line was the recommendation of Code Complete , which stated that such code was easier to understand. To make each expression do one and only one, so it is very easy to see very quickly at a glance what is happening in the code.

I personally like to have return types in variables to make it easier to check (or even change) in the debugger.

One answer indicated that there was a difference in the generated code. I sincerely doubt that with an optimizing compiler.

The drawback prior to C ++ 11 for multiple lines is that you need to know the return type for the variable declaration. For example, if the return is changed from bool to int, then depending on the types you use, you may have a truncated value in a local variable (which can lead to a malfunction). If compiling with C ++ 11 enabled, this can be solved using the auto keyword, as in:

 auto AwesomeResult = SomeBoolReturningFunc() ; if ( AwesomeResult ) { //do some other, more important stuff } 

Stroustrup C ++ 4th edition recommends putting the variable declaration in the if statement itself, for example:

 if ( auto AwesomeResult = SomeBoolReturningFunc() ) { //do some other, more important stuff } 

His argument is that this limits the scope of the variable as much as possible. Regardless of whether it is more readable (or debugged) is a challenge to the solution.

+1
source

The version of AwesomeResult can be faster if SomeBoolReturningFunc () is quite slow, and you can use AwesomeResult more than once rather than calling SomeBoolReturningFunc ().

+1
source

Placing the function inside or outside the if statement does not matter. No effort or loss. This is because the compiler automatically creates a stack space for the return value - regardless of whether you explicitly specified the variable.

+1
source

In the performance tuning that I did, this could be thought of only at the last stage of the shaving cycle after eliminating a number of significant performance problems.

+1
source

All Articles