这个Java算法如何更快
我有一个循环,它将布尔 LinkedList 按 8 位分割并返回缓冲区中每个字节的 ASCII 值。该函数返回字符串缓冲区。
如果 LinkedList 的大小很大,那么这段代码会非常慢。我尝试用简单的循环来更改迭代器,但它仍然很慢。
这个算法怎么能真正快呢?也许使用多线程?
注意: linkedList 的大小并不总是能被 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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这些建议将为您提供足够的性能,同时保持代码简单和可读。首先使用这些更改进行测试,如果不满足您的性能要求,则慢慢引入其他答案中建议的优化技术
BitSet
而不是LinkedList
StringBuilder输出;
而不是字符串输出;
StringBuilder缓冲区;
而不是字符串缓冲区;
Integer.valueOf()
而不是Integer.parseInt
。我认为valueOf
对低于 128 的值使用缓存。These suggestions will give you enough performance still keeping the code simple and readable. First test using these changes and if doesn't meet your performance requirements then slowly introduce optimization techniques suggested in other answers
BitSet
instead ofLinkedList<Boolean>
StringBuilder output;
instead ofString output;
StringBuilder buffer;
instead ofString buffer;
Integer.valueOf()
instead ofInteger.parseInt
.valueOf
uses cache for values below 128 i think.使用使用预期输出容量初始化的
StringBuilder
:使用按位运算而不是
parseInt()
,如下所示:Use
StringBuilder
initialized with expected capacity for output:Use bitwise operations instead of
parseInt()
, something like this:字符串连接速度很慢,尤其是对于大型列表(因为字符串是不可变的,因此必须复制它们,这需要一些时间,并且每个副本也需要更多空间)。使用
StringBuilder
而不是String
进行追加。换句话说:buffer
和output
应该是StringBuilder
实例。String concatenation is slow especially for large lists (since strings are immutable they have to be copied around which takes some time and each copy requires more space as well). Use a
StringBuilder
instead of aString
to append to. In other words:buffer
andoutput
should beStringBuilder
instances.正如其他人建议的那样 - 使用 BitSet。对于其余的,我认为下面的方法非常有效:
这可能是一个不使用字节计数器的更好的替代方案:
As others suggested - use BitSet. For the rest, I think the method below is pretty efficient:
Here is possibly a better alternative that does not use byte counter:
尝试将缓冲区保持为
int
。我的意思是代替
Also use
StringBuilder
进行输出。这是一个很小的变化,但总是一点点。Try to keep buffer as an
int
. I meaninstead of
Also use
StringBuilder
for output. This is a small change here but always a bit.请尝试以下操作:
Try the following:
使用 StringBuffer 或 stringBuilder 而不是 String 作为缓冲区和输出变量。
字符串变量是不可变的,因此每个操作都会在堆中创建一个新实例,而 StringBuilder 和 StringBuffer 则不然。
Use StringBuffer or stringBuilder instead of String for your buffer and output vars.
String vars are immutable, so every operations creates a new instance in heap, while StringBuilder and StringBuffer are not.