Is it faster to use an array or bit-bit for multiple booleans?

1) Is it faster to get an array of 32 Boolean values ​​on a 32-bit processor or get access to 32 bits in one word? (Suppose we want to check the value of the Nth element and use either a bitmask (the Nth bit is set) or an integer N as the index of the array.)

It seems to me that the array will be faster, because all common computer architectures initially work at the word level (32 bits, 64 bits, etc., are processed in parallel) and access to the bits of the subword requires additional work.

I know that different compilers will present things differently, but it seems that the underlying hardware architecture will dictate the answer. Or does the answer depend on the language and compiler?

A, 2) The response to speed is canceled if this array represents the state that I pass between the client and server? This question came to mind when reading the question " How to use a bit / bit operator to control the state of an object? "

PS Yes, I could write code to test it myself, but then the SO community wouldn’t be able to play!

+1
source share
6 answers

For question # 1: Yes, on most 32-bit platforms, the boolean array should be faster, because you just load every 32-bit value in the array and check it for 0. If you use one word, you will have everything that works, plus the overhead of bit-driving.

№2: , , , , .

+2

, , , , , , . , , , . , , , , , .

+4
+3

, 0!= ( (1 < )) :

00401000  mov         eax,1 
00401005  shl         eax,cl 
00401007  and         eax,1 

[index] bool []:

00401000  movzx       eax,byte ptr [ecx+eax]

, , , bool [].

+1

, , , . , , , .

, .

0

, , , , .

, , , / .

0

All Articles