I’m trying to improve performance for an application that does a lot of bit operations.
One operation is:
c |= (1 << i)
in which I want to set a single bit in a byte. I was thinking of maybe using a lookup table to get each of 8 values to OR in. Would an array access be faster or slower than just the original bit operation?
Or perhaps is there a better way?
On any modern computer architecture, the shift operation will complete in a single CPU cycle. A table lookup might take as little as one cycle if the table is in the CPU cache; otherwise it will take much, much longer (possibly millions of times longer, if the memory has been swapped to disk).
On older ARM processors (9 series and earlier), the shift takes two cycles (assuming
iisn’t a constant); in that case, a table lookup could be faster – a single cycle, if the table’s base register is already set up, and the table is in the cache, and the processor has a cache at all.Some very old processors didn’t have fast shift hardware, in which case a lookup might be considerably faster – especially as CPU speeds tended to be the same as memory speeds back then.
So if you find yourself in the 1980s, or writing the firmware for a hard drive, then this might be useful; but make sure you measure it to be sure.