Don't use Scanf()

Richard Harter g-rh at cca.CCA.COM
Tue Mar 15 21:37:01 AEST 1988


Re: Should one use puts versus always using printf.

Some years ago someone (I don't have a reference anymore) took a
fortran compiler and modified it to compile formatted writes rather
than interpreting them.  The gains in execution time were modest, on
the order of 1-2%, at best.  Corresponding gains can be expected in
C.  While it is true that using puts rather than printf when applicable
is faster, the execution time efficiency gained is not particularly
important.

As a matter of programming style, it is preferable (all other things
being equal) to use a single standard mechanism rather than a variety
of mechanisms.  It makes programs more complicated to write, maintain,
and read if a variety of mechanisms are used for the same function.
This is a simplicity versus execution time tradeoff.

In our situation (SMDS not CCA) portability is an issue.  By policy we
restrict the use of library routines to a minimum -- the only library
functions which are not carefully isolated are printf and fprintf.  If
I don't use puts() I don't have to know whether it runs the same and
interacts the same with printf() on all machines and operating systems
that we deal with.  This policy is reasonable for out situation; it is
not necessarily relevant for others.

I would also note that if execution time for I/O is truly a concern
(and it sometimes is) then one should look at doing formatting by hand,
i.e. building output lines directly and writing whole lines only.

In summary, this is one of those little efficiencies that is not worth
bothering about in most applications.
-- 

In the fields of Hell where the grass grows high
Are the graves of dreams allowed to die.
	Richard Harter, SMDS  Inc.



More information about the Comp.lang.c mailing list