任何人都可以在 Scala 中优化以下语句:
// maybe large
val someArray = Array(9, 1, 6, 2, 1, 9, 4, 5, 1, 6, 5, 0, 6)
// output a sorted list which contains unique element from the array without 0
val newList=(someArray filter (_>0)).toList.distinct.sort((e1, e2) => (e1 > e2))
既然性能很关键,有没有更好的办法呢?
谢谢你。
最佳答案
这条简单的代码是迄今为止最快的代码之一:
someArray.toList.filter (_ > 0).sortWith (_ > _).distinct
但到目前为止,明显的赢家是 - 由于我的测量 - Jed Wesley-Smith。也许如果 Rex 的代码是固定的,它看起来会有所不同。
典型免责声明 1 + 2:
这里是底层 benchcoat-code and the concrete code 生成图形(gnuplot)。 Y 轴:以秒为单位的时间。 X 轴:数组中的 100 000 到 1 000 000 个元素。
更新:
在发现 Rex 代码的问题后,他的代码与 Jed 的代码一样快,但最后一个操作是将他的 Array 转换为 List(以填充我的基准接口(interface))。使用
var result = List [Int]
, 和 result = someArray (i) :: result
加快了他的代码速度,因此它的速度大约是 Jed-Code 的两倍。另一个可能有趣的发现是:如果我按照过滤器/排序/不同 (fsd) => (dsf, dfs, fsd, ...) 的顺序重新排列我的代码,所有 6 种可能性都没有显着差异。
关于arrays - 将 Scala 数组转换为唯一排序列表的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8160037/