使用bitset :: operator []等價於使用bitset :: test還是存在一些基礎優化?bitset :: operator [] == false/true或bitset :: test?
也就是說,這兩個循環等價嗎?
使用位集::操作符[]:
static const int UP = 0;
static const int DOWN = 1;
for(int i = 1; i < KEY_MAX; ++i) {
if(_handler && (_prevKey[i] == UP && _curKey[i] == DOWN)) {
_handler->EnqueueEvent(new KeyboardKeyDownEvent(i));
}
if(_handler && (_prevKey[i] == DOWN && _curKey[i] == DOWN)) {
_handler->EnqueueEvent(new KeyboardKeyPressEvent(i));
}
if(_handler && (_prevKey[i] == DOWN && _curKey[i] == UP)) {
_handler->EnqueueEvent(new KeyboardKeyUpEvent(i));
}
}
使用位集::測試():
static const bool UP = false;
static const bool DOWN = true;
for(int i = 1; i < KEY_MAX; ++i) {
if(_handler && (_prevKey.test(i) == UP && _curKey.test(i) == DOWN)) {
_handler->EnqueueEvent(new KeyboardKeyDownEvent(i));
}
if(_handler && (_prevKey.test(i) == DOWN && _curKey.test(i) == DOWN)) {
_handler->EnqueueEvent(new KeyboardKeyPressEvent(i));
}
if(_handler && (_prevKey.test(i) == DOWN && _curKey.test(i) == UP)) {
_handler->EnqueueEvent(new KeyboardKeyUpEvent(i));
}
}