假设您期望高比率false
结果您可以进行快速“预检查”以快速隔离此类情况:
如果有点在a
已设置但未在任何设置中设置d
, e
and f
then a
不能等于其中任何一个。
因此像
int pre_eq3(int a, int b, int c, int d, int e, int f){
int const mask = ~(d | e | f);
if ((a & mask) && (b & mask) && (c & mask)) {
return false;
}
return eq3(a, b, c, d, e, f);
}
could speed it up (8 operations instead of 9 17, but much more costly if the result will actually be true
). If mask == 0
then of course this won't help.
如果有很高的概率,这可以进一步改进a & b & c
设置了一些位:
int pre_eq3(int a, int b, int c, int d, int e, int f){
int const mask = ~(d | e | f);
if ((a & b & c) & mask) {
return false;
}
if ((a & mask) && (b & mask) && (c & mask)) {
return false;
}
return eq3(a, b, c, d, e, f);
}
Now if alla、b 和 c 有位设置,而 d、e 和 c 没有任何位设置,我们很快就出局了。