Divide and C
Stephen Clamage
steve at taumet.com
Fri Mar 29 02:32:02 AEST 1991
karln!karln at uunet.uu.net (Karl Nicholas) writes:
> I get a number. I need to use this number to index into a
> 2 dimensional array.
> EX: int array[10][10];
> val = 24;
> ++array[val/10][val%10];
> This is the only way I know how to do this.
> My problem with it is that is requires TWO divides.
Some compilers are smart enough to recognize a pattern like
array[val/10][val%10]
and do a single division, using the resulting quotient and remainder.
Some are smart enough to recognize this pattern across statement
boundaries, e.g.,
i = a/b; j = a%b;
If your compiler is not this smart and you know that the time spent in
the extra division is making your program unacceptably slow, you can
try the Standard C library function div(), which takes a numerator
and denominator and returns a structure containing the quotient and
remainder. Use of this function might turn out to be slower than
the extra divide.
--
Steve Clamage, TauMetric Corp, steve at taumet.com
More information about the Comp.lang.c
mailing list