3
我在做与Java StringBuilder的测试,尤其是其在AbstractStringBuilder类作为遵循实施替换(INT,INT,字符串)功能:可能的性能增强了AbstractStringBuilder
public AbstractStringBuilder replace(int start, int end, String str) {
if (start < 0)
throw new StringIndexOutOfBoundsException(start);
if (start > count)
throw new StringIndexOutOfBoundsException("start > length()");
if (start > end)
throw new StringIndexOutOfBoundsException("start > end");
if (end > count)
end = count;
int len = str.length();
int newCount = count + len - (end - start);
if (newCount > value.length)
expandCapacity(newCount);
System.arraycopy(value, end, value, start + len, count - end);
str.getChars(value, start);
count = newCount;
return this;
}
的Arraycopy函数调用将“移动”部分值字符数组的内容以为后面注入的str内容(str.getChars(value,start))留出空间。从我的角度来看,如果str长度与字符数组中要覆盖的空间不匹配,则只应该执行此arraycopy。
显然是一个非常绝望考虑这个性能问题,虽然有较大的字符数组(> 50万个字符)和一个StringBuilder类替代含铅可衡量的绩效改进arraycopy做测试。
在Windows 32位平台上用java 6测试了一个相同的StringBuilder实例,用于几百万个替换调用。
你认为这是不重要的,错误还是我完全错过了某些东西?
不重要的,绝对。 – vulkanino 2012-03-08 13:07:44
向Oracle/Sun提交请求并查看他们的意见。我不认为这是一个“错误”,因为代码有效。把它称为一个可能的增强,你可能会进一步。我也认为你可能完全错过了一些东西。你可能正在考虑一个通常不是有效的角落案例。我现在没有时间来验证你的思路。 – duffymo 2012-03-08 13:09:26
我怀疑arraycopy是否足够聪明以避免不必要的工作;唯一的性能改进来自避免确定工作不必要的检查。实际上,我认为你有一个观点。 – 2012-03-08 13:14:20