scala - How can I speed up flatten? -
scala - How can I speed up flatten? -
i have method:
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 re-create ( usage = if (head.machine == previous.machine) head.counter - previous.counter else head.usage) current :: tail } reverse } }
where reportshavingcalculatedcounter
of type: val reportshavingcalculatedcounter: scala.collection.immutable.iterable[scala.collection.immutable.indexedseq[scala.collection.immutable.map[strin g,com.agilexs.machinexs.logic.reportdatahelper]]]
.
this code works perfectly. problem reportshavingcalculatedcounter
has maps within whom sum of reportdatahelper
objects (map values) 50 000 entries , flatten.flatten
takes 15s processed.
i've tried 2 flat maps that's same (time consuming). there way improve this? (please ignore foldleft
or reverse
; if remove issue still present, time consuming 2 flatten
).
update: i've tried different scenario:
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 }
and takes: counter2 (15.075s)
.
i can't imagine scala slow. slowest part v.values.foreach
.
scala
Comments
Post a Comment