I have a function that must return true of false depending on the value of a member enum representing the type of operator.
I'm wondering what would be the fastest between the following options, as I'm not sure of what implicit optimizations the compiler is going to do, if any.
inline bool isBinaryOper( void ) const // Fastest i assume. { static const bool arr[] = { true, // E_PLUS true, // E_MINUS true, // E_MULTIPLY true, // E_DIVIDE false, // E_LPARENT false, // E_RPARENT false, // E_COMMA false // E_SEMICOLON }; return arr[ size_t( this->_eType ) ]; // Assuming values are valid indexes. } Or :
inline bool isBinaryOper( void ) const { switch( this->_eType ) { case E_PLUS : return true; case E_MINUS : return true; case E_MULTIPLY : return true; case E_DIVIDE : return true; case E_LPARENT : return false; case E_RPARENT : return false; case E_COMMA : return false; case E_SEMICOLON : return false; default : ... }; } Or, which I guess is very similar to the previous one :
inline bool isBinaryOper( void ) const { if ( this->_eType == E_PLUS ) return true; else if ( this->_eType == E_MINUS ) return true; // etc... } Which one would be the fastest, and why ?
E_constants were all powers of base-2 and could combine into a bitmask. Then:return !(this->_eType & (E_LPARENT | E_RPARENT | ...))_eTypeonce,inline bool isBinaryOper() const { return _isBinary; }.