Reputation: 311
EDIT: In C++ If I had, 8 flags I need to test. Which would be more efficient in terms of processing speed:
Checking 8 bool variables in a single if statement or having a char representing the flags, with 1 bit for each flag and using a single bitwise operation in a single if statement?
If I had 1 flag to test, would that make a difference?
Upvotes: 2
Views: 2712
Reputation: 9468
Well, the best option is to compile the code to assembler and examine it.
The 8 bool flags need 8 registers. This is a lot. The 8 bit flags need only one register. If You put all flags in one register, it will be very fast to test.
Upvotes: 0
Reputation: 63481
I'm generally a fan of bitwise flags, but I find it's usually helpful to have some support functions:
inline bool TestAll( char flags, char value ) {
return (flags & value) == value;
}
inline bool TestAny( char flags, char value ) {
return (flags & value) != 0;
}
inline bool TestAllExclusive( char flags, char value ) {
return flags == value;
}
inline bool TestAnyExclusive( char flags, char value ) {
return (flags & value) != 0 && (flags & ~value) == 0;
}
You can also do similar functions to take flag masks, making all that magical stuff easier to read.
If you have your have a truth value to test, this is a winner in terms of speed because you're just working with constants.
typedef enum EmotiveFlags {
E_ELATED = 0x01,
E_JOYOUS = 0x02,
E_NONPLUSED = 0x04,
E_BEWILDERED = 0x08,
E_ANNOYED = 0x10,
E_PUZZLED = 0x20,
E_ENRAGED = 0x40,
E_AMUSED = 0x80
} EEmotiveFlags;
if( TestAny(flags, E_JOYOUS | E_ELATED | E_AMUSED) ) {
//...
}
else if( TestAll(flags, E_ANNOYED | E_ENRAGED) && !TestAll(flags, E_AMUSED) ) {
//...
}
Upvotes: 2
Reputation: 477
This question really boils down to the compiler. It depends on how it references the bits to check, however, I believe it is safe to say that as long as you are checking more than one bit, a char would be faster.
The amount of clock cycles that it takes to run through the char would be noticeably shorter than individual variables because the flags are stacked adjacent in memory, thus eliminating the requirement of memory addressing.
If you are wanting a noticeable speed difference though, you aren't going to get it unless you change your method of checking.
Upvotes: 1
Reputation: 882466
If you have a known value for all bits that you wanted to check, it would probably be faster to compare a char
:
if (bitval == 0x82) ...
If you wanted to check certain bits, ignoring others, it may be faster to keep the "bits" in separate variables:
if ((bitval7 == 1) && (bitval1 == 1)) ...
Or it may be faster to simply mask out the bits you don't care about:
if ((bitval & 0x82) == 0x82) ...
If you were really concerned, you could opt to store them in both ways (provided you kept them synchronised) and choose depending on how many bits you were interested in.
But I'd consider that serious overkill because, in all honesty, the speed difference is likely to be so minimal as to be irrelevant.
If you're going to optimise, you'll almost always get better return on investment for macro things like algorithm selection, rather than micro-optimisations like separating booleans or changing loop directions.
Don't get me wrong, these micro-optimisations can make a difference in certain circumstances, but they're not usually the best way.
Upvotes: 5