greenber@acf4.UUCP (12/09/84)
<> Which (in general!) generates "better" code within a switch or loop? if (condition) { code.... break; } other code.... break; -------------------------------OR--------------- if (condition) { code.... } else { other code..... } break; Ross M. Greenberg @ NYU ----> allegra!cmcl2!acf4!greenber <----
thomas@utah-gr.UUCP (Spencer W. Thomas) (12/10/84)
In article <13900013@acf4.UUCP> greenber@acf4.UUCP writes: >Which (in general!) generates "better" code within a switch or loop? > > if (condition) > { > code.... > break; > } > other code.... > break; >-------------------------------OR--------------- > if (condition) > { > code.... > } > else > { > other code..... > } > break; Which generates code that is easier to read and modify!? -- =Spencer ({ihnp4,decvax}!utah-cs!thomas, thomas@utah-cs.ARPA) <<< Silly quote of the week >>>
berry@zinfandel.UUCP (Berry Kercheval) (12/10/84)
In article <13900013@acf4.UUCP> greenber@acf4.UUCP writes: > >Which (in general!) generates "better" code within a switch or loop? > > if (condition) > { > code.... > break; > } > other code.... > break; >-------------------------------OR--------------- > if (condition) > { > code.... > } > else > { > other code..... > } > break; > > >Ross M. Greenberg @ NYU ----> allegra!cmcl2!acf4!greenber <---- IN GENERAL it is impossible to tell, as it is VERY dependent on the particular compiler you have, and the machine you are generating code for. The only way is to try both, look at the assembly output and make a decision. Why didn't you do that? -- Berry Kercheval Zehntel Inc. (ihnp4!zehntel!zinfandel!berry) (415)932-6900
guy@rlgvax.UUCP (Guy Harris) (12/11/84)
Both if (condition) { code.... break; } other code.... break; and if (condition) { code.... } else { other code..... } break; generate identical code with the 4.2BSD VAX PCC when the -O flag is on, which isn't too surprising since those code sequences are equivalent. That probably requires that the compiler be smart enough to recognize jumps to jumps and the like, but any compiler that does those optimizations should generate the same code for both. Guy Harris {seismo,ihnp4,allegra}!rlgvax!guy
mjs@alice.UUCP (M. J. Shannon, Jr.) (12/11/84)
The question asked was, `Which generates better code?': if (exp) { stuff; goto, continue, break, or return; } any of the other 3; or: if (exp) { stuff; goto, etc. } else { any of the other 3; } The answer should be that it makes no difference, and for the compilers I have access to, it doesn't if you use the -O flag. If you don't use an optimizer, then the 2nd case above might generate a branch to a branch to implement `one of the other 3'. However, if the compiler is sufficiently `smart', then it will recognize that the `else' is superfluous, and nicely fail to generate the branch to branch. Thus, the answer is that the first form above will generate no worse code than the 2nd, but any `reasonable' compiler will generate the same code for both. -- Marty Shannon UUCP: {alice,research}!mjs (rabbit is dead; long live alice!) Phone: 201-582-3199
henry@utzoo.UUCP (Henry Spencer) (12/11/84)
Any decent C compiler should generate exactly the same code for these two cases, and the if-then-else is probably clearer to read. (Indecent C compilers are utterly unpredictable, so I can't address them...) -- Henry Spencer @ U of Toronto Zoology {allegra,ihnp4,linus,decvax}!utzoo!henry
greenber@acf4.UUCP (12/12/84)
<> Gee, Dad, I'm sorry....I'll never ask for the keys to the car again! Actually, I looked at the output of the compiler of this 4.2 machine, of my BDS C-compiler on CP/M, and on Lattice on MS-DOS. I'll just bet that there are other machines out there that I personally might not have access to. Ross M. Greenberg @ NYU ----> allegra!cmcl2!acf4!greenber <----
chuqui@nsc.UUCP (Chuqui[The Time Traveller]) (12/14/84)
>Any decent C compiler should generate exactly the same code for these >two cases, and the if-then-else is probably clearer to read. (Indecent >C compilers are utterly unpredictable, so I can't address them...) Indecent compilers should have their development arrested... chuq (gonna wish I didn't say that tomorrow.... *urgh*) -- From behind the bar at Callahan's: Chuq Von Rospach {allegra,cbosgd,decwrl,hplabs,ihnp4,seismo}!nsc!chuqui nsc!chuqui@decwrl.ARPA The evil that men do lives after them; the good it oft interred with their bones. So let it be with Caesar. The noble Brutus hath told you Caesar was ambitious... And Brutus is an honorable man
ix269@sdcc6.UUCP (12/19/84)
both forms are of the same speed as best as I can determine, but the latter form is much more readable at a glance (Note that this is a style judgement, and purely my own opinion). switch (whosit) { case 0: if (A) { dothis(); break; } elsethis(); break; case 1: if (B) { dothis(); } else { elsethis(); } break; }
ix269@sdcc6.UUCP (12/19/84)
fig() Might it be noted that if-else pairs are accepted as one statement? Break is a jump (not machine phrase) out of the switch, so the big question ends up being will your optimizer discover that one of the conditionals is jumping twice (if-else as aversed to if-break-break)? The source code for the if-else is better looking, and perhaps even the cost of an extra jump might not be worth the ugliness (unless this is a kernal ugly). This leaves things in a different light than merely style, but welcome to a higher level language :-). switch (A) { case 0: if (B) { /* this */ break; } /* else */ break; case 1: if (B) { /* this */ } else { /* that */ } break; }
ix269%Nosc@sdcc6.UUCP (01/07/85)
if (condition); else ... There it is. Why do you think that I ever wrote such a thing? I did reply to the which is better question, but that was about a question of a preformance cludge someone wanted to do inside of a case statement to replace 2 breaks. I supported having the two breaks, as they will get redone correctly in most every case (4.2,version [3-7],perhaps more). Nice letter, but why me? Jim.