Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #5557

Re: efficient or clever way

Message-ID <4DECC653.2C1C@mindspring.com> (permalink)
Date 2011-06-06 08:21 -0400
From pete <pfiland@mindspring.com>
Organization PF
Newsgroups comp.lang.c
Subject Re: efficient or clever way
References <70c4add5-a342-40b8-b037-079b1bedc153@x3g2000yqj.googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

efficient or clever way "dr.oktopus" <blindwilly@freeonline.zzn.com> - 2011-06-06 01:29 -0700
  Re: efficient or clever way China Blue Angels <chine.bleu@yahoo.com> - 2011-06-06 02:12 -0700
    Re: efficient or clever way Chris H <chris@phaedsys.org> - 2011-06-06 12:50 +0100
      Re: efficient or clever way "Kleuskes & Moos" <kleuske@xs4all.nl> - 2011-06-06 11:38 -0700
    Re: efficient or clever way Anand Hariharan <mailto.anand.hariharan@gmail.com> - 2011-06-08 13:48 -0700
      Re: efficient or clever way China Blue Angels <chine.bleu@yahoo.com> - 2011-06-08 16:16 -0700
      Re: efficient or clever way "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2011-06-12 01:10 -0700
  Re: efficient or clever way pete <pfiland@mindspring.com> - 2011-06-06 08:21 -0400
    Re: efficient or clever way Voice Of Truth <oops@uhm.wow> - 2011-06-06 20:48 +0200
      Re: efficient or clever way Keith Thompson <kst-u@mib.org> - 2011-06-06 11:55 -0700
        Re: efficient or clever way Voice Of Truth <oops@uhm.wow> - 2011-06-06 20:58 +0200
      Re: efficient or clever way pete <pfiland@mindspring.com> - 2011-06-06 19:10 -0400
        Re: efficient or clever way Voice Of Truth <oops@uhm.wow> - 2011-06-07 05:54 +0200
  Re: efficient or clever way cri@tiac.net (Richard Harter) - 2011-06-06 15:13 +0000
  Re: efficient or clever way Keith Thompson <kst-u@mib.org> - 2011-06-06 09:35 -0700
  Re: efficient or clever way Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-06-08 20:34 +0000

csiph-web