I wanted to test which one is faster for my problem: boolean[]
or BitSet
. I need to update a seperate matrix for every pair of indices which are true. e.g. [true, true, false, true, false]
results in [0][1]++
, [0][3]++
and [1][3]++
.
Using boolean[]
:
for (int i = 0; i < (booleanArray.length - 1); i++) {
if (booleanArray[i]) {
for (int j = i + 1; j < booleanArray.length; j++) {
if (booleanArray[j]) {
matrix[i][j]++;
}
}
}
}
Using BitSet
:
for (int i = bitSet.nextSetBit(0); i != -1; i = bitSet.nextSetBit(i + 1)) {
for (int j = bitSet.nextSetBit(i + 1); j != -1; j = bitSet.nextSetBit(j + 1)) {
matrix[i][j]++;
}
}
Iterating through both showed that boolean[]
seems to be faster (1.5 vs 3 sec), but updating the matrix needs so much more time (20 sec vs. 6 sec!). How is this possible? How can I improve the performance using boolean[]
?