CoMPMStR wrote:Here's another submission:
#4: Isn't the ternary operator much faster than calling the std::max function?
- Code: Select all
unsigned long ulMax = 0UL;
for ( size_t I = 0UL; I < vVector.size(); ++I ) {
ulMax = (ulMax <= vVector[I]) ? vVector[I] : ulMax;
}
std::max() is a templated function so it will resolve to basically the same thing.
Is there something else you could do?
CoMPMStR wrote:#5: If we're just toggling an option on or off (0 or 1), instead of doing ulSwitch=!ulSwitch; just do a simple xor by 1. ulSwitch^=1;, I know for a fact that all bitwise operations are faster than anything else and the logical not (!) is NOT a bitwise operation.
Correct.
If you want to switch back and forth between 0 and 1, ^= 1 is the hands-down fastest way to go about it.
troublesh00ter wrote:
- Code: Select all
ulX = ulIndex & (16UL - 1UL);
ulY = (ulIndex - ulX) / 16UL;
As 16 is a power of 2 we can use a bitwise AND instead of %. Something that I learned from this quiz is that the compiler will put 15 instead of 16 - 1, normally I would write 15UL but I suppose this way the intention is clearer.
A thank you to CoMPMStR (if the answer is correct) for sparking a brain cell with the 'bitwise operation' comment.
Is there something more you can do to this?
L. Spiro