I have this very big vector<unsigned char> bitmap
that I use as a bitmap. Most of time it has like millions or billions of entries. So every bit in a unsigned char stands for a free(0) or used(1) block in my program. My function to set those bits looks like this:
int setNextFreeIDX_In(std::vector<unsigned char> & bitmap){
for(int i=0; i<bitmap.size(); i++){
for(int bit=0; bit<8; bit++){
if(!(bitmap[i] & (1 << bit))){ //72.33% <- this is what the Xcode Profile says
turnOn(bit, bitmap[i]);
return (i*8) + bit;
}
}
}
return -1;
}
So after profiling my Code with Xcode instruments, it says that this function takes a lot of time. (about 25 minutes! when working with about 4.5 gb of blocks and every block is 4096 bytes)
do you see anything that could take that much time. Or maybe there is a better way to do this!?
I already tried to do this with the iterator instead of for(int i....
but it still takes a lot of time. Here is my turnOn function:
void turnOn(int bitNumber, unsigned char & byte){ //75.00% <- Profiler says this
byte |= 0x01 << bitNumber; //turn on the N-th bit
}