Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Mon, 06 Jun 2011 07:21:34 -0500 Message-ID: <4DECC653.2C1C@mindspring.com> Date: Mon, 06 Jun 2011 08:21:39 -0400 From: pete Reply-To: pfiland@mindspring.com Organization: PF X-Mailer: Mozilla 3.04Gold (WinNT; I) MIME-Version: 1.0 Newsgroups: comp.lang.c Subject: Re: efficient or clever way References: <70c4add5-a342-40b8-b037-079b1bedc153@x3g2000yqj.googlegroups.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 42 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.154.218.17 X-Trace: sv3-7CBubkQG4YncvrsBILa+GYKOnG7YrT412RM5lJ0NyZLusnNJR++GgBL/qXxbBGipSgTKF4p4yChgJHs!//xexVohVempzqbyVNRpO4UhbidN06RgeCNPdzyHg37SswoDsqUQBDbSlo7+E4959H+oeTw+7EGC!PzXfExx7aQ== X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2228 Xref: x330-a1.tempe.blueboxinc.net comp.lang.c:5557 dr.oktopus wrote: > > Hello, > walking an array is a common construct in programming. > In C language, I see two common practises. > > 1: > > for (p = array, pend = array + size ; p < pend ; ++p) > /* do something */ > > 2: > > count = size; > p = array; > while (count--) { > /* do something here */ > ++p; > } > > In complex contests, keeping a count variable could > be more readable (IMO) than having a variable acting > as a sentinel for the array bound (I'm speking of pieces > of codes where array navigation is not from the start > to the end, or inside Duff's devices, for example). > My question is: is this code really inefficient than > first approach (since it has to dec a variable every > cycle, more than test it) or current cpus could handle a sort > of decrement and test instr that do it all at the same time? > Thanks, > wily It all depends on the cpu. Comparing two variables against each other may or may not be slower than a decrement plus a comparison against constant zero. A decrement plus a comparison against constant zero, is a single instruction in Microchip assembly. -- pete