more questions about efficient C code
Chris Torek
chris at umcp-cs.UUCP
Thu Jun 27 17:38:58 AEST 1985
> This example
[ if ((fp = fopen(...)) == NULL) vs fp = fopen(...); if (fp == NULL) ]
> bothers me. I'm not sure what all makes up the fopen subroutine,
> but the addition or subtraction of one measly 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?
Heck, 500 nanoseconds is significant, isn't it? :-)
Besides, you can save four bytes per test. I'll bet it makes a disk
block or two of difference. And we all need all the space we can get,
right? :-)
Seriously, the difference in the fopen call is pretty small, but
the difference in something like
for (...)
for (...) {
if ((j = (k >> 2) - m) == 0)
k++;
}
can be important. Once you get into the habit of testing the results
of an assignment, it doesn't even look funny anymore.
--
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