Path: utzoo!attcan!uunet!pyrdc!pyrnj!rutgers!att!ulysses!ggs From: ggs@ulysses.homer.nj.att.com (Griff Smith) Newsgroups: comp.lang.c Subject: Re: Array indexing vs. pointers... Summary: a few counter-examples Message-ID: <10699@ulysses.homer.nj.att.com> Date: 5 Oct 88 21:45:50 GMT References: <836@proxftl.UUCP| <3105@hubcap.UUCP> <1700@dataio.Data-IO.COM> <8630@smoke.ARPA> Organization: AT&T Bell Laboratories, Murray Hill Lines: 56 In article <8630@smoke.ARPA|, gwyn@smoke.ARPA (Doug Gwyn ) writes: | In article <1700@dataio.Data-IO.COM| bright@dataio.Data-IO.COM (Walter Bright) writes: | |Here's some of the things I do: | | I have to take exception to most of these micro-efficiency tweaks. | Most of them have no effect on the code generated by even a moderately | good compiler, and nearly all of them make the code more obscure. Agreed, but a few might deserve some further comment... | > o Replace stuff like, | > if (a) | > x = b; | > else | > x = c; | > with, | > x = a ? b : c; | | I don't know of any compiler that wouldn't produce the same code | for these two cases. Write whichever is more readable in context. When given the second definition, C++ 2.0 (beta 4) produces the following error message if `x', `a' and `b' are of class `foo' and `c' is a function call that returns a `foo'. "xx.c", line 133: sorry, not implemented: temporary of class foo with destructor needed in ? expression | > o Combine printfs, | > printf("aksdf aksjdhf kahdf jhdsfhj\n"); | > printf(" asdkljfhkajshdf djfh kjahsdfkja h\n"); | > Convert to, | > printf("aksdf aksjdhf kahdf jhdsfhj\n\ | > asdkljfhkajshdf djfh kjahsdfkja h\n"); | | Thereby introducing a bug (exercise for the student). | The difference in time between these is negligible, | but if you're really tweaking for efficiency puts() | might have been better, depending on the implementation. Sequences of printf can be harder to read, however. The following example may show this better: (void) fprintf(stderr, "\ Usage: sltread [options] input-drive-name\n\ Options: [-l] [-s] [-v] [-x] [-z] [-t tape-volume-number]\n\ [-d dataset-name] [-n dataset-number]\n\ [-i ignore-count] [-r record-count]\n"); I grant that this is vulnerable to code hackers and naive pretty-printers messing up the alignment. On the other hand, it is easier to see how everything lines up. -- Griff Smith AT&T (Bell Laboratories), Murray Hill Phone: 1-201-582-7736 UUCP: {most AT&T sites}!ulysses!ggs Internet: ggs@ulysses.att.com