I seek the assessment of which code block is more efficient in Java at detecting the byte sequence of
00 00 00 01
and
00 00 01
in a byte array.
Version 1
for (int i = 0; i < frame.length - 4; i++) {
if (frame[i] == 0) {
if (frame[i + 1] == 0) {
if (frame[i + 2] == 0) {
if (frame[i + 3] == 1) {
// found marker 00 00 00 01
// do work here
}
} else {
if (frame[i + 2] == 1) {
// found marker 00 00 01
// do work here
}
}
}
}
}
Version 2
for (int i = 0; i < frame.length - 4; i++) {
if (frame[i] == 0 && frame[i + 1] == 0 && frame[i + 2] == 0 && frame[i + 3] == 1) {
// found marker 00 00 00 01
// do work here
} else if (frame[i] == 0 && frame[i + 1] == 0 && frame[i + 2] == 1) {
// found marker 00 00 01
// do work here
}
}