有效的方式来斯卡拉数组转换为独特的排序清单 [英] Efficient way to convert Scala Array to Unique Sorted List

查看:120
本文介绍了有效的方式来斯卡拉数组转换为独特的排序清单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以优化下面的语句在Scala中:

Can anybody optimize following statement in 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))

由于性能是至关重要的,有没有更好的办法?

Since the performance is critical, is there a better way?

感谢您。

推荐答案

这简单的线条是最快的codeS到目前为止之一:

This simple line is one of the fastest codes so far:

someArray.toList.filter (_ > 0).sortWith (_ > _).distinct

但明显的赢家至今 - 由于我的测量 - 杰德韦斯利·史密斯。也许,如果雷克斯code是固定的,它看起来不同。

but the clear winner so far is - due to my measurement - Jed Wesley-Smith. Maybe if Rex' code is fixed, it looks different.

典型免责声明1 + 2

Typical disclaimer 1 + 2:


  1. 我修改了codeS接受阵列并返回一个列表。

  2. 典型标杆的考虑:

    • 这是随机的数据,平均分配。 100万的元素,我创建了0和1万美元之间的100万整数数组。因此,与更多或更少的零,并或多或少重复,它可能会有所不同。

    • 这可能取决于机器等上。我使用了单核CPU,Intel的Linux-32位,JDK-1.6,斯卡拉2.9.0.1

下面是底层 benchcoat- code和混凝土code 产生图形(gnuplot的)。 Y轴:时间(秒)。 X轴:100 000到阵列中的1 000 000元

Here is the underlying benchcoat-code and the concrete code to produce the graph (gnuplot). Y-axis: time in seconds. X-axis: 100 000 to 1 000 000 elements in Array.

与雷克斯code发现问题后,他的code是一样快,杰德的code,但最后的操作是他阵的转型到一个列表(fullfill我的基准接口)。使用 VAR的结果=列表[INT] =结果的someArray(I)::结果加快了code,所以它是大约快一倍的Jed- code。

After finding the problem with Rex' code, his code is as fast as Jed's code, but the last operation is a transformation of his Array to a List (to fullfill my benchmark-interface). Using a var result = List [Int], and result = someArray (i) :: result speeds his code up, so that it is about twice as fast as the Jed-Code.

另外,也许是有趣的,发现是:如果我重新安排我的code过滤器的顺序/排序/不同(FSD)=>(DSF,DFS,消防处,...),所有6个可能性别ŧ显著不同。

Another, maybe interesting, finding is: If I rearrange my code in the order of filter/sort/distinct (fsd) => (dsf, dfs, fsd, ...), all 6 possibilities don't differ significantly.

这篇关于有效的方式来斯卡拉数组转换为独特的排序清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆