I have this loop which splits a Boolean LinkedList by 8 bits and return the ASCII value of each byte in a buffer. The function return the string buffer.
This code is extremely slow if the LinkedList’s size is big. I try to change the Iterator with a simple looping, but it’s still slow.
How can this algorithm be really fast ? Maybe with multi-threading ?
Note: The size of the linkedList is not always divisible by 8.
public String toString(){
String buffer = "";
String output = "";
LinkedList<Boolean> bits = this.bits;
for(Iterator it = this.bits.iterator(); it.hasNext();){
if(buffer.length() >= 8){
output += (char)Integer.parseInt(buffer, 2);
buffer = "";
}
buffer += ((Boolean)it.next() == false) ? "0" : "1";
}
if(buffer != "")
output += (char)Integer.parseInt(buffer, 2);
return output;
}
Use
StringBuilderinitialized with expected capacity for output:Use bitwise operations instead of
parseInt(), something like this: