Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 12 Nov 1997 09:16:58 -0500 (EST)
From:      "Adrian T. Filipi-Martin" <atf3r@cs.virginia.edu>
To:        Luigi Rizzo <luigi@labinfo.iet.unipi.it>
Cc:        hackers@freebsd.org
Subject:   Re: A stylistic question...
Message-ID:  <Pine.SUN.3.90.971112090720.11574B-100000@stretch.cs.Virginia.edu>
In-Reply-To: <199711120858.JAA06510@labinfo.iet.unipi.it>

next in thread | previous in thread | raw e-mail | index | archive | help
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().  

	Why do yo feel that you need the "%s" though?  Just make the
"blah..." your format string.  The parsing of the format string is pretty
efficient.  It reads a char and then switch()'s on it to what to do. 
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. 

	Adrian
--
adrian@virginia.edu        ---->>>>| If I were stranded on a desert island, and
System Administrator         --->>>| I could only have one OS for my computer,
Neurosurgical Visualzation Lab -->>| it would be FreeBSD.  Think about it.....
http://www.nvl.virginia.edu/     ->|      http://www.freebsd.org/




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.SUN.3.90.971112090720.11574B-100000>