Yet Another Silly Question

Dave Decot decot at hpisod2.HP.COM
Tue May 23 15:05:38 AEST 1989


> 	int a[MAX];			int a[MAX];
> 	int i;				int *p;
> 	for (i=0; i<MAX; ++i)		for (p=&a[0]; p<&a[MAX]; ++p)
> 		a[i] = 0;			*p=0;
> 
> 	...
> 
> ... On some compiler/machine combinations, this will run faster,
> because the scaling operation and base/offset addition have been
> eliminated; on others it may run slower, because a specific addressing
> mode cannot be used.

Note that the scaling operation has not necessarily been completely
eliminated; it may have become hidden in "++p" because increments by 1
may be much faster than increments by sizeof(*p).

Anyway, if one is interested in permitting high performance, one might as
well give the compiler complete latitude to do this efficiently by
rewriting this as:

	static struct { int i[MAX]; } a_, zero;
	int *a = &a_.i[0];

	...

	a_ = zero;

  :-) :-) :-) :-) :-) :-) :-) :-)

Dave "Equal rights for arrays, NOW!" Decot



More information about the Comp.lang.c mailing list