-2

I know that this question has been asked and answered here, among other places, but none of the answers touched on the reason I was given (years ago) for the adverse performance impact of "unnecessary" curly braces. In that light, I'd like to revisit this issue.

Briefly, I was told that:

if(true) 
    do_something();

is more performant than

if(true)
{
   do_something();
}

The reason given, as I recall, was because the compiler would introduce a branch in the assembled code in the second case which could have a cumulative effect.

Now, I can spell complier, but beyond that I know very little about how they operate, so is the above theory true? Was it ever true?

Community
  • 1
  • 1
Michael J.
  • 349
  • 1
  • 10
  • 6
    You need to not listen to whoever told you that. – NathanOliver Jun 03 '16 at 16:58
  • 4
    Whoever told you that is either incompetent or malicious. – Mysticial Jun 03 '16 at 16:59
  • 6
    You were told blatant rubbish. Kick that person in the ass please (for incompetence). – πάντα ῥεῖ Jun 03 '16 at 16:59
  • @πάνταῥεῖ, three times. – SergeyA Jun 03 '16 at 17:03
  • 1
    @SergeyA I already told you _it takes five_ ;-) ... – πάντα ῥεῖ Jun 03 '16 at 17:03
  • "would introduce a branch in the assembled code in the second case which could have a cumulative effect": independently of all the other comments, I can't really make sense of this sentence. Cumulative to what ??? –  Jun 03 '16 at 17:08
  • I can see four possibilities: a) you're misremembering; b) you misunderstood; c) someone was wrong but you believed them because you thought they weren't clueless; d) you mistook a joke for a factual statement. – molbdnilo Jun 03 '16 at 19:05
  • `the compiler would introduce a branch in the assembled code in the second case which could have a cumulative effect` what? It won't introduce a branch in the first branch? If a branchless instruction was used for the second case, so is the first case – phuclv Jun 04 '16 at 03:25

3 Answers3

1

so is the above theory true?

No.

Was it ever true?

No.
Not even with the worst ancient compilers lacking clever optimization strategies I could imagine these statements would emit different assenbly code.

These would simply be emitted as a call to the do_something subroutine.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
1

In unoptimized as well as optimized generated code, those two statements would translate exactly to the same. The braces create a new scope where nothing is declared, so nothing can be implemented differently, semantically speaking. (Unless a dumb compiler would manage an empty stack frame for this inner block ?!)

0

The "facts" you were given are completely madeup. There is no performance reduction at all, as people have already mentioned in the very link you gave.

There is no way for us to explain the reasoning of something that's not true.

nvoigt
  • 75,013
  • 26
  • 93
  • 142