Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site umcp-cs.UUCP Path: utzoo!watmath!clyde!bonnie!akgua!whuxlm!harpo!decvax!linus!philabs!cmcl2!seismo!umcp-cs!chris From: chris@umcp-cs.UUCP (Chris Torek) Newsgroups: net.lang.c Subject: Re: more questions about efficient C code Message-ID: <397@umcp-cs.UUCP> Date: Fri, 21-Jun-85 20:23:45 EDT Article-I.D.: umcp-cs.397 Posted: Fri Jun 21 20:23:45 1985 Date-Received: Mon, 24-Jun-85 04:47:52 EDT References: <474@crystal.UUCP> Distribution: net Organization: U of Maryland, Computer Science Dept., College Park, MD Lines: 43 All of these depend on your compiler. I'm going to answer them for 4BSD Vax PCC. > 1) Is the "var = (bool expr) ? : expr1 : expr2" construct faster > than just a good old if statement? Depends. PCC generates moves into r0 for expr1 and expr2, followed by a move from r0 to var. If var is already a register, this is clearly slower. If it's a complex pointer dereference, it could be faster. > 2) Are for loops faster than while loops? No. > 3) Is there really much to be gained by using assignment within > a boolean expression, for example, is > > if ((fp = fopen("foo", "r") == NULL) { > > really that much faster than > > fp = fopen("foo", "r"); > if (fp == NULL) { Again, depends: if "fp" is not a register, the assignment+test is faster, since you end up branching after the assignment from r0 to "fp". If it is a register, it comes out the same. (At least after optimization, usually). The first form is never slower, anyway. > 4) Are tests for zero more efficently written as > > if (var) { > > rather than as > > if (var != 0) { No. -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 4251) UUCP: seismo!umcp-cs!chris CSNet: chris@umcp-cs ARPA: chris@maryland