pointers for speed

Dave P. Schaumann dave at cs.arizona.edu
Tue Feb 26 10:36:24 AEST 1991


In article <1998 at gold.gvg.tek.com> shaunc at gold.gvg.tek.com (Shaun Case) writes:
>I know that when you repeatedly access something like
>
>foo.a.b.c.d[11].value
>
>it is better to declare a (register) pointer, assign it the address
>of foo.a.b.c.d[11].value, and do manipulations on that, since it
>is faster.

Oh really?  Seems to me that &foo.a.b.c.d[0] is a constant that can be
computed *at compile time*.  That means that (unless your comiler is
totally brain-dead) accessing foo.a.b.c.d[0] should be as fast as accessing
a normal element of an array of the same type.

Yes, calculating the address does take a bit longer, but that calculation is
done once by the compiler, and is known forever more.

Now, if you had foo->a->b->c->d, you would certainly want to keep the
address of d in a variable, rather than indirecting 4 times.

-- 
Dave Schaumann      | Is this question undecidable?
dave at cs.arizona.edu |



More information about the Comp.std.c mailing list