more questions about efficient C code
Chris Torek
chris at umcp-cs.UUCP
Sat Jun 22 10:23:45 AEST 1985
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 at umcp-cs ARPA: chris at maryland
More information about the Comp.lang.c
mailing list