more questions about efficient C code
Joe Durnavich
jjd at wlcrjs.UUCP
Tue Jun 25 15:30:55 AEST 1985
>> 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.
This example bothers me. I'm not sure what all makes up the
fopen subroutine, but the addition or subtraction of one measely
assignment statement *has* to be negligible when compared to what
goes on in fopen. Even if that code is in the innermost of inner
loops, the "optimization" will still be unnoticable. I know
it's just an example of an assignment within a boolean expression,
but I see a *lot* of programs with that same code. Are we really
gaining anything, or is it merely psychological?
Joe Durnavich
ihnp4!wlcrjs!jjd
No part of this message may be reproduced, stored in a retrieval system, or
transmitted, in any form or by any means, without the permission of the author.
More information about the Comp.lang.c
mailing list