zhengruifeng edited a comment on pull request #30468: URL: https://github.com/apache/spark/pull/30468#issuecomment-732096836
Finally, I found that [Guava.Ordering](https://github.com/apache/spark/pull/30468/commits/7dd2b919b7149e5064cc1bd90100db52feb6cba2) seems much more efficient than `BoundedPriorityQueue`. (see [Selecting top k items from a list efficiently in Java / Groovy](https://www.michaelpollmeier.com/selecting-top-k-items-from-a-list-efficiently-in-java-groovy)). ``` /** select top indices based on values. */ private[recommendation] class TopSelector(val values: Array[Float]) { import scala.collection.JavaConverters._ private val indexOrdering = new GuavaOrdering[Int] { override def compare(left: Int, right: Int): Int = { Ordering[Float].compare(values(left), values(right)) } } def selectTopKIndices(iterator: Iterator[Int], k: Int): Array[Int] = { indexOrdering.greatestOf(iterator.asJava, k).asScala.toArray } } ``` Compared to `BoundedPriorityQueue`, we do not need to create many object references like `Tuple2` here.   ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
