Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 13 Nov 1997 13:29:00 +1000
From:      Stephen McKay <syssgm@dtir.qld.gov.au>
To:        "Adrian T. Filipi-Martin" <atf3r@cs.virginia.edu>, Luigi Rizzo <luigi@labinfo.iet.unipi.it>
Cc:        freebsd-hackers@freebsd.org, syssgm@dtir.qld.gov.au
Subject:   Re: A stylistic question... 
Message-ID:  <199711130329.NAA24138@ogre.dtir.qld.gov.au>
In-Reply-To: <Pine.SUN.3.90.971112090720.11574B-100000@stretch.cs.Virginia.edu> from "Adrian T. Filipi-Martin" at "Wed, 12 Nov 1997 14:16:58 %2B0000"
References:  <Pine.SUN.3.90.971112090720.11574B-100000@stretch.cs.Virginia.edu>

next in thread | previous in thread | raw e-mail | index | archive | help
On Wednesday, 12th November 1997, "Adrian T. Filipi-Martin" wrote:

>On Wed, 12 Nov 1997, Luigi Rizzo wrote:
>
>> I have always wondered about this: most if not all programs, and some
>> pieces of the kernel as well (e.g. userconfig.c)
>> have a menu/usage function which is written like this:
>> 
>>     usage()
>>     {
>> 	printf( "bla bla bla...\n" );
>> 	printf( "bla bla bla...\n" );
>> 	printf( "bla bla bla...\n" );
>> 	...
>> 	printf( "bla bla bla...\n" );
>>     }
>> 
>> instead of what in my opinion would be much better:
>> 
>>     usage()
>>     {
>> 	printf( "%s", "bla bla bla...\n"
>> 		"bla bla bla...\n"
>> 		...
>> 		"bla bla bla...\n");
>>     }
>> 
>> yes the code savings are modest (5-10 bytes/call ? but in the kernel
>> or boot blocks they still matter...) but at runtime the second
>> approach is faster since the format string must be parsed only once
>> and it is the shortest possible.
>> 
>> Any reason not to use the second method ?
>
>	Not really.  It will compile into slightly smaller code and it
>will have better runtime performance since there will only one function
>call to printf().  

>That's one comparison per character.  Printing a string using "%s" will
>still need to do one comparison with each character when looking for '\0'. 
>The overhead of the indexed jump is probably not worth worying about. 

Yow!  You guys are really counting the time it takes to print the usage
string?  How's your best time on an optimised idle loop?

Step back a moment and consider the purpose of your code.  If it is code
for a boot block and is severely space constrained, consider using one puts()
or even write() (maybe you can get rid of stdio completely).  If it is a
time critical subroutine, use some clever algorithm or even some assembler.

But for a humble usage message, and indeed most normal code, the absolutely
number one most important requirement is human readability.  Your processor
and memory subsystem is getting faster every year, but your programmers
(yourself and others here) are just getting older and grumpier. :-)

Pick whichever one looks most pleasing and easiest to understand.  For my
money, that is the first one.  You are wasting your precious programmer
time considering such marginal time and space costs.

Stephen.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199711130329.NAA24138>