我做了一个中值滤波算法,我想优化它.目前它需要大约1秒来过滤2MM线(一个文件读入ArrayList元素),我试图将其减少到更少(可能是一半的时间?)我正在使用ArrayLists进行算法并最小化嵌套循环的使用以及为了避免增加时间,但我仍然无法达到低于0.98秒的上限.
这是一个执行中值过滤器的代码片段:
//Start Filter Algorithm 2
int index=0;
while(index
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
break; //Break if there is not enough elements for the filtering to work
}
}
发生了什么事情,我正在为我提供的filterSize循环遍历元素arraylist.然后我将元素添加到临时(tempElements)arraylist,使用Collections.sort()对其进行排序(这是我想要避免的),找到中值并将其添加到我的最终输出arraylist.然后我清除tempElements arraylist并继续循环我的循环,直到由于缺少元素而不能再过滤(小于filterSize).
我只是在寻找一种优化它并加快速度的方法.我试图使用TreeSet但我无法从它获得索引的值.
谢谢