From owner-freebsd-hackers Wed Nov 12 19:31:42 1997 Return-Path: Received: (from root@localhost) by hub.freebsd.org (8.8.7/8.8.7) id TAA08366 for hackers-outgoing; Wed, 12 Nov 1997 19:31:42 -0800 (PST) (envelope-from owner-freebsd-hackers) Received: from ren.dtir.qld.gov.au (firewall-user@ns.dtir.qld.gov.au [203.108.138.66]) by hub.freebsd.org (8.8.7/8.8.7) with ESMTP id TAA08224 for ; Wed, 12 Nov 1997 19:30:36 -0800 (PST) (envelope-from syssgm@dtir.qld.gov.au) Received: by ren.dtir.qld.gov.au; id NAA06621; Thu, 13 Nov 1997 13:46:51 +1000 (EST) Received: from ogre.dtir.qld.gov.au(167.123.8.3) by ren.dtir.qld.gov.au via smap (3.2) id xma006614; Thu, 13 Nov 97 13:46:37 +1000 Received: from localhost.dtir.qld.gov.au (localhost.dtir.qld.gov.au [127.0.0.1]) by ogre.dtir.qld.gov.au (8.8.7/8.8.7) with SMTP id NAA24138; Thu, 13 Nov 1997 13:29:00 +1000 (EST) Message-Id: <199711130329.NAA24138@ogre.dtir.qld.gov.au> X-Authentication-Warning: ogre.dtir.qld.gov.au: localhost.dtir.qld.gov.au [127.0.0.1] didn't use HELO protocol To: "Adrian T. Filipi-Martin" , Luigi Rizzo cc: freebsd-hackers@freebsd.org, syssgm@dtir.qld.gov.au Subject: Re: A stylistic question... References: In-Reply-To: from "Adrian T. Filipi-Martin" at "Wed, 12 Nov 1997 14:16:58 +0000" Date: Thu, 13 Nov 1997 13:29:00 +1000 From: Stephen McKay Sender: owner-freebsd-hackers@freebsd.org X-Loop: FreeBSD.org Precedence: bulk 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.