Is an array accessible faster than modular division?

I do not know what overhead arises when searching in an int array. What will be better (in C #):

a = aLookup[i];
b = (a % 6) == 5;
c = (b ? a+1 : a-1) >> 1;  // (a + 1) / 2 or (a - 1) / 2

or

a = aLookup[i];
b = bLookup[i];
c = cLookup[i];

Will array search really save so much time for bor c?

Edit: I have profiled it in several ways. As a result, finding arrays is almost four times faster.

+3
source share
4 answers

AND:

  • depends on
    • item type
    • array length
    • cache location
      • affinity for the processor, L2 cache size
    • cache duration (or, more importantly: how many times is it used to turn off caching?)

B:

0
source

. , . ,

c = (b ? a+1 : a-1) >> 1;

, - , . .

, , .

+3

O (1) , .

, , , , .

+1

, % , , - % :

while (x >= n) x -= n;

but they can make assumptions about the range x (which are checked in debug builds) if you do not do 10,000+ of them per second, I would not worry about that

0
source

All Articles