0

我有这个方法:

val reportsWithCalculatedUsage = time("Calculate USAGE") {
  reportsHavingCalculatedCounter.flatten.flatten.toList.groupBy(_._2.product).mapValues(_.map(_._2)) mapValues { list =>
    list.foldLeft(List[ReportDataHelper]()) {
      case (Nil, head) =>
        List(head)
      case (tail, head) =>
        val previous = tail.head
        val current = head copy (
          usage = if (head.machine == previous.machine) head.counter - previous.counter else head.usage)
        current :: tail
    } reverse
  }
}

哪里reportsHavingCalculatedCounter是类型:val reportsHavingCalculatedCounter: scala.collection.immutable.Iterable[scala.collection.immutable.IndexedSeq[scala.collection.immutable.Map[Strin g,com.agilexs.machinexs.logic.ReportDataHelper]]]

此代码完美运行。问题是它reportsHavingCalculatedCounter内部有地图,其对象总和ReportDataHelper(地图值)约为 50 000 个条目,处理flatten.flatten时间约为 15 秒。

我也尝试过使用 2 张平面地图,但这几乎相同(耗时)。有什么办法可以改善这一点吗?(请忽略foldLeftor reverse;如果我删除该问题仍然存在,最耗时的是那些 2 flatten)。

更新:我尝试过不同的场景:

val reportsHavingCalculatedCounter2: Seq[ReportDataHelper] = time("Counter2") {
  val builder = new ArrayBuffer[ReportDataHelper](50000)
  var c = 0
  reportsHavingCalculatedCounter.foreach { v =>
    v.foreach { v =>
      v.values.foreach { v =>
        c += 1
        builder += v
      }
    }
  }
  println("Count:" + c)
  builder.result
}

它需要:Counter2 (15.075s).

我无法想象 scala 很慢。这是最慢的部分v.values.foreach

4

0 回答 0