Scala Future [T]头顶重吗?

时间:2017-08-20 23:39:38

标签: scala

我编写了一个合并排序来测试scala Future [T]类型的异步计算性能。

我有一个4核CPU,所以我期望异步计算比同步计算快大约4倍,因为我使用完全cpu功能(由于子任务的大小相同,停止时间应该很小)。但是,结果显示异步合并排序比正常合并排序慢。

我是不是写了并发或者只是因为Future [T]开销?有人能帮我解释一下吗?

package kai.concurrent

import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.Random

object MergeSort {
  lazy val regressThreadhold = 10000

  def mergeSortedList[T](a: Seq[T], b: Seq[T])(implicit ord: Ordering[T]): Seq[T] = {
    def loop(a: Seq[T], b: Seq[T], acc: Seq[T]): Seq[T] = {
      if (a.isEmpty && b.isEmpty) acc
      else if (a.isEmpty) b.reverse ++: acc
      else if (b.isEmpty) a.reverse ++: acc
      else if (ord.lt(a.head, b.head)) loop(a.tail, b, a.head +: acc)
      else loop(a, b.tail, b.head +: acc)
    }

    loop(a, b, Seq()).reverse
  }

  def mergeSortAsync0[T](x: Seq[T])(implicit ord: Ordering[T]): Future[Seq[T]] =
    if (x.size <= regressThreadhold) Future(mergeSort(x)) else {
      val (left, right) = x.splitAt(x.size / 2)
      val Seq(leftSorted, rightSorted) = Seq(left, right).map(seq => Future(mergeSortAsync0(seq)).flatten)
      leftSorted.zip(rightSorted).map(pair => mergeSortedList(pair._1, pair._2))
    }

  def mergeSortAsync[T](x: Seq[T])(implicit ord: Ordering[T]): Seq[T] =
    Await.result(mergeSortAsync0(x), Duration.Inf)

  def mergeSort[T](x: Seq[T])(implicit ord: Ordering[T]): Seq[T] =
    if (x.size <= 1) x else {
      val (left, right) = x.splitAt(x.size / 2)
      val (leftSorted, rightSorted) = (mergeSort(left), mergeSort(right))
      mergeSortedList(leftSorted, rightSorted)
    }
}

object MergeSortTest extends App {

  import kai.util.ProfileUtil.TimeResult

  val seq: Vector[Double] = (1 to 1000000).map(i => Random.nextDouble()).toVector
  val seqMergeSortAsync = MergeSort.mergeSortAsync(seq) withWallTimePrinted "mergeSortAsync"
  val seqMergeSort = MergeSort.mergeSort(seq) withWallTimePrinted "mergeSort"
  val seqSort = seq.sorted withWallTimePrinted "sorted"
  println(seqSort == seqMergeSort && seqMergeSort == seqMergeSortAsync)
}

输出:

mergeSortAsync elapsed time: 3186 ms

mergeSort elapsed time: 3300 ms

sorted elapsed time: 581 ms

true

1 个答案:

答案 0 :(得分:4)

我已经复制了您的测试并通过JMH运行(使用sbt-jmh)。我在测试中使用预定义的 var ctxLine = document.getElementById("line-chart").getContext("2d"); //var myLineChart; //myLineChart; if(window.bar != undefined) window.bar.destroy(); window.bar = new Chart(ctxLine, {}); 作为底层执行上下文。

结果:

scala.concurrent.ExecutionContext.Implicits.global

您可以在此处看到,运行并行版本比顺序版本快约x1.5,而Scala排序比顺序合并排序快x6倍。

需要记住的是,在进行这些微基准测试时,需要考虑很多因素。通常最好让JMH处理JVM运行时为您提供的细微之处。

plugins.sbt:

[info] Benchmark                          Mode  Cnt  Score   Error  Units
[info] MergeSortTest.benchMergeSortAsync  avgt   25  1.534 +–’ 0.212   s/op
[info] MergeSortTest.benchMergeSortSync   avgt   25  2.325 +–’ 0.437   s/op
[info] MergeSortTest.benchScalaSort       avgt   25  0.382 +–’ 0.006   s/op

build.sbt:

addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.2.27")

测试代码:

enablePlugins(JmhPlugin)