You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
someBigArray.search(element) is slow because ArrayOps is not a IndexedSeq (but a IndexedSeqOptimized), so that collection.Searching does not use binary search.
Reproducing Code
Running attached file [^slow.scala], searching on an Array with 1000000 elements is 10000x slower than searching on a WrappedArray.
searchArray:3116.342782 ms
searchWrappedArray:0.273139 ms
In the implicit method search, Array is converted to ArrayOps by IsSeqLike:
importscala.collection.generic.IsSeqLike
implicitly[IsSeqLike[Array[Int]]].conversion(Array(1)).getClass
// class scala.collection.mutable.ArrayOps$ofInt
ArrayOps is a IndexedSeqOptimized but not a IndexedSeq since methods like IndexedSeq#filter and others must returns a IndexedSeq; therefore, binary search is not used.
Resolution
Rewriting from case _: IndexedSeq[A] to case _: IndexedSeqLike[A, _] fixes this. Try attached file [^fast.scala].
searchArray:0.170193 ms
searchWrappedArray:0.267205 ms
The text was updated successfully, but these errors were encountered:
Description
someBigArray.search(element)
is slow becauseArrayOps
is not aIndexedSeq
(but aIndexedSeqOptimized
), so thatcollection.Searching
does not use binary search.Reproducing Code
Running attached file [^slow.scala], searching on an
Array
with 1000000 elements is 10000x slower than searching on aWrappedArray
.Cause
The object
Searching
is implemented as follows:In the implicit method
search
,Array
is converted toArrayOps
byIsSeqLike
:ArrayOps
is aIndexedSeqOptimized
but not aIndexedSeq
since methods likeIndexedSeq#filter
and others must returns aIndexedSeq
; therefore, binary search is not used.Resolution
Rewriting from
case _: IndexedSeq[A]
tocase _: IndexedSeqLike[A, _]
fixes this. Try attached file [^fast.scala].The text was updated successfully, but these errors were encountered: