Path: utzoo!utgpu!watmath!clyde!att!ulysses!andante!alice!ark From: ark@alice.UUCP (Andrew Koenig) Newsgroups: comp.lang.c Subject: Re: Simple C Question (was: down) Message-ID: <8563@alice.UUCP> Date: 19 Dec 88 14:48:14 GMT References: <192@broadway.UUCP> <6959@pyr.gatech.EDU> Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 33 In article <6959@pyr.gatech.EDU>, byron@pyr.gatech.EDU (Byron A Jeff) writes: > I think I like one of the following two better: > chcnt += (c == '\n') ? 2 : 1; > chcnt += 1 + (c == '\n'); > Any comments one which one of the ones we've seen is most efficient? If the problem is stated this way: count all the characters and count one extra for each newline then it seems to me that the most direct solution is: chcnt++; if (c == '\n') chcnt++; I suspect this is also the fastest on most machines. For example, here are instruction counts for the compiler on my machine: c=='\n' c!='\n' statement 6 6 chcnt += (c == '\n') ? 2 : 1; 5 4 chcnt += 1 + (c == '\n'); 4 3 chcnt++; if (c == '\n') chcnt++; This assumes that c and chcnt are both in registers. -- --Andrew Koenig ark@europa.att.com