【发布时间】:2015-08-10 12:48:16
【问题描述】:
我做了一个中值滤波算法,我想优化它。目前过滤 2MM 行大约需要 1 秒(将文件读入 ArrayList elements),我正试图将其减少到更少(可能是一半时间?)嵌套循环也是为了避免增加时间,但是我仍然无法达到低于 0.98 秒的最高值。
这是一个执行中值过滤的代码 sn-p:
//Start Filter Algorithm 2
int index=0;
while(index<filterSize){
tempElements.add(this.elements.get(index+counter)); //Add element to a temporary arraylist
index+=1;
if(index==filterSize){
outputElements.add(tempElements.get((filterSize-1)/2)); //Add median Value to output ArrayList
tempElements.clear(); //Clear temporary ArrayList
index = 0; //Reset index
counter+=1; //Counter increments by 1 to move to start on next element in elements ArrayList
}
if(elementsSize-counter <filterSize){
break; //Break if there is not enough elements for the filtering to work
}
}
发生的情况是,我正在循环遍历 elements 数组列表以获取我提供的 filterSize。然后我将元素添加到临时(tempElements)数组列表中,使用Collections.sort()(这是我想要避免的)对其进行排序,找到中间值并将其添加到我的最终输出数组列表中。然后我清除 tempElements 数组列表并继续循环,直到由于缺少元素(小于 filterSize)而无法再过滤。
我只是在寻找一种方法来优化它并让它更快。我尝试使用 TreeSet,但无法从中获取索引处的值。
谢谢
【问题讨论】:
标签: java algorithm performance sorting arraylist