3

Is there a difference between the following pieces of code:

  1. while (a==b);
  2. while (a==b) {;}

Does it make an impact on compiler or execution time?

Background for this questions: Currently the FW uses code 1, where a or b is actually a global variable and the FW hangs/waits in a particular core till the condition is made true by another core. We would like to simulate this code in SystemC and this "infinite" while loop is not under SystemC context and therefore the SystemC scheduler will not know that this thread is waiting for another, leading to a deadlock.

Therefore we want to replace the code with: while (a==b) {CONTEXT_SWITCH(1);}

where for target build we have:

#define CONTEXT_SWITCH(x)

and for systemC build we have:

#define CONTEXT_SWITCH(x)   wait(x) //where wait is a systemC wait

This code in the case of target build becomes code 2. Wanted to know if this can impact performance somehow?

unkulunkulu
  • 11,576
  • 2
  • 31
  • 49
subtle85
  • 57
  • 3
  • Why `while (a==b) {;}` and not just `while (a==b) {}`? You can try the latter if the former proves to be too slow :) Seriously, though, there's no difference. Whether it "makes an impact" (if your compiler somehow behaves differently) - only you can tell. – AnT stands with Russia May 11 '13 at 15:41

4 Answers4

8

Does it make an impact on compiler or execution time?

No. It is exactly the same thing. There won't be any difference at run-time, and the difference in compilation time is the negligible difference for parsing {;}.

If you have to choose one for any reason, pick the one that makes its intent clearer for you.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • 1
    I would always pick the {;} over just ; since one looks like an accident where you have an infinite loop and you do not know why, and the other looks like intention. – SinisterMJ May 11 '13 at 15:00
  • @subtle85: Glad I could help. If this answers your question, please consider marking the answer as accepted (or any other answer by different users that you may prefer). – Andy Prowl May 11 '13 at 15:24
1

An good mainstream compiler will generate the exact some assembly code. So no, there is no difference in actual run-time performance.

It is a matter of perception as to which one you choose. Pick up the one the coding standards you have mandate.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Even the stupidest, most naively implemented compiler in the world should generate the same assembly for the two constructs, they're syntactically and semantically identical. – This isn't my real name May 12 '13 at 06:39
0

No, it should have absolutely no impact on performance.

Braces do not generate code as such. The compiler uses them to determine the beginning and end of a compound statement, nothing else. To the compiler, a compound or single statement is dealt with the same way - the braces are just so that the compiler knows what you want to go as the statement for the while-loop.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
0

Both will have the same execution time, when we write while(a==b); The compiler will internally convert it into while(a==b){;}. So it doesn't matter in which form you wrote it.

reshma
  • 641
  • 1
  • 11
  • 18