- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我做了一个中值滤波算法,想优化一下。目前过滤 2MM 行(读入 ArrayList elements
的文件)大约需要 1 秒,我正在尝试将其减少到更少(可能是一半时间?)我正在使用 ArrayLists 作为我的算法并尽量减少嵌套循环的使用以避免时间增加,但我仍然无法达到低于 0.98 秒的最高点。
这是执行中值滤波器的代码片段:
//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
}
}
我正在为我提供的 filterSize
遍历 elements
数组列表。然后我将元素添加到临时 (tempElements
) arraylist,使用 Collections.sort()
对其进行排序(这是我想要避免的),找到中值并将它添加到我的最终输出数组列表中。然后我清除 tempElements
arraylist 并继续执行循环,直到由于缺少元素(小于 filterSize
)而无法再进行过滤。
我只是在寻找一种优化它并使其更快的方法。我尝试使用 TreeSet,但无法从中获取索引处的值。
谢谢
最佳答案
Java Collections.sort()
实现在排序(双枢轴快速排序)时速度最快。
这里的问题不在于具体的细节,而在于您要进行排序!您只需要找到中位数,并且有线性算法(排序是对数线性的)。参见 selection一些灵感。您可能需要自己编写代码,因为我认为 Java 库没有任何可用的公共(public)实现。
我建议的另一件事是使用固定大小的数组(创建一次)而不是 ArrayList
。由于您事先知道过滤器的大小,这会给您带来小幅速度提升。
此外,我看不出避免 for 循环如何以任何方式提高性能。除非您分析它并证明它是正确的做法,否则我会尽可能编写最易读的代码。
最后,TreeSet
或任何其他类型的排序数据结构也无济于事,因为 n 插入的时间复杂度是对数线性的。
关于java - 有没有比 Java 中的 Collections.sort() 更快的东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31920027/
这个fn是吗: function isplainobj ( obj ) { return Object.prototype.toString.call( obj ) === "[object
我尝试创建一个我没有经验的小 bash 脚本。我尝试做类似的事情: #!/bin/bash statut="na" if [ $proc = 0 ]; then statut = "close
我想重写 HighLine 的几个方法来自定义我的控制台,目前我的代码如下所示: cmd = ask("#{@prompt_label} #{@prompt_separator} ",
鉴于下面的 HTML,我尝试使用 jQuery 来匹配所有具有类“foo”的跨度的列表项,并且该跨度应包含文本“relevant”。 Some text relevant Some more
我拥有一个 5 美元 20GB SSD Digital Ocean Droplet,它几乎用完了 Docker 镜像和容器的空间。 有没有办法购买一个卷,将其连接到服务器并安全地将所有 Docker
我有这样的表: id name number result stuff stuff stuff stuff 我只想将 class = "red" 添加到
我需要计算两点之间的距离,但不是以常规方式。我需要知道“东西距离”+“南北距离”。我想这比常规的“乌鸦飞翔”计算更简单,但我仍然不知道如何做到这一点。 我想使用 MySQL 查询来执行此操作,并且最好
#include #include #include typedef struct dict_pair { void *key; void *value; struct dict_p
为什么当我尝试通过 将 char[] word 写入控制台时会发生这种奇怪的事情 Console.WriteLine(word); 我得到了一个正确的结果,但是当我写的时候 Console.Write
一个简单的例子: class C{} class B{ @Inject C c; void doSomething(){ System.out.println(c);
我想做某事,但不确定如何描述它。我有这门课 public class Company { private List _persons; private Person GetPersonByNa
我正在尝试实现我自己的 qDebug()样式调试输出流,这基本上是我目前所拥有的: struct debug { #if defined(DEBUG) template std::os
所以我正在为我的应用程序编写一个搜索功能,代码如下: - (void) selectObject: (NSString *)notes{ [axKnotes removeAllObjects]
我想在 Rust 中匹配一种复杂的继承式东西: struct Entity { pub kind: EntityKind, } pub enum EntityKind { Player
我是 SQL 新手。这没有返回任何结果...... SELECT media.id as mediaid, media.title as mediatitle, media.description
在微型 SDCard 上写入 Android things 图像并将该卡插入 Raspberry Pi 3 Model B 并打开电源,启动时显示“Auto config Please wait”然后
这是一个常见的但是文本出现在框的右侧,不是极右但几乎是这样 h3: ................................................ .................
#include #include #include #include #include int main(int argc, string argv[]) { if(argc >
所以我试图让一些东西相互堆叠。首先,查看工作链接会有所帮助,您会看到问题所在: http://brownbox.net.au/clients/matchlessphotography/ 现在我需要使用
我想在禁用 javascript 时在我的网站顶部显示一条消息(就像在 SO 上一样),但在谷歌浏览器上不起作用 最佳答案 看起来是这样。 您可以使用 javascript 隐藏“noscript”消
我是一名优秀的程序员,十分优秀!