M
Morris Dovey
Richard said:You didn't save anything with this approach - you only left the
possibility for a caller to crash the system by calling it with a zero
option.
Hmm - my earlier comment stands. I might take a different view if
this were a widely-used library function likely to be used
blindly by coders who needed to be protected from themselves.
Even if you had done an explicit zero check (or needed to) then the
overhead was nothing compared to the display loop.
A set of test compiles and assembly assemblies showed that in my
environment with gnu toolchain provided some interesting results.
I've copied the assembly listings into the matching C files and
put 3 test cases at:
http://www.iedu.com/c/ta1.c (Ben's)
http://www.iedu.com/c/ta2.c (Mine)
http://www.iedu.com/c/ta3.c (Mine, with a zero check)
The file Ben posted and the file I posted assemble to the same
length (87 bytes). The file I posted above with a check for
(arraylen > 0) was only 73 bytes and actually produced less
entry/exit code.
There is an execution-time cost in calling this function to
determine whether it should be run or not. If your programming
environment is such that built-in protection from failure to do
necessary checks can be a problem, then I'm inclined to agree
that the cost is justified. In other environments where that's
not a problem (but where performance /is/) then I'm inclined to
minimize overhead processing by avoiding even the small amount of
overhead inherent in the call by moving the test outside the
function.
I won't claim that either approach is _always_ right, but thought
you might find it interesting that my version got shorter when
code to perform the test was added.