我怎样才能加速变平?

时间:2013-02-20 18:16:04

标签: scala

我有这个方法:

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张平面地图,但这几乎是相同的(耗费时间)。有没有办法改善这个? (请忽略foldLeftreverse;如果我删除该问题仍然存在,则最耗时的是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

0 个答案:

没有答案