Created
September 28, 2023 14:11
-
-
Save autodidacticon/82a2ba9907ecddfd287e34c40db04a6b to your computer and use it in GitHub Desktop.
scala.collection.parallel.mutable.ParMap vs java.util.concurrent.ConcurrentHashMap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> :load test.sc | |
val args: Array[String] = Array() | |
Loading test.sc... | |
import java.lang.System.nanoTime | |
import java.util.concurrent.ConcurrentHashMap | |
import scala.collection.mutable | |
import scala.collection.parallel.mutable.{ParArray, ParHashMap} | |
import scala.jdk.CollectionConverters.ConcurrentMapHasAsScala | |
val javaMap: scala.collection.concurrent.Map[Long,Long] = Map() | |
val scalaMap: scala.collection.parallel.mutable.ParHashMap[Long,Long] = ParHashMap() | |
val testSize: Int = 1200000 | |
val iterations: Int = 100 | |
val javaDurations: scala.collection.mutable.ListBuffer[Long] = ListBuffer() | |
val scalaDurations: scala.collection.mutable.ListBuffer[Long] = ListBuffer() | |
val javaAvg: Double = 1.48506236771E7 | |
val javaVariance: Double = 3.1534672699274938E13 | |
val javaStdDev: Double = 5615574.120183522 | |
java.util.concurrent.ConcurrentHashMap: average filter time: 1.48506236771E7 | |
java.util.concurrent.ConcurrentHashMap: min filter time: 7929000 | |
java.util.concurrent.ConcurrentHashMap: max filter time: 161625250 | |
java.util.concurrent.ConcurrentHashMap: filter time variance: 3.1534672699274938E13 | |
java.util.concurrent.ConcurrentHashMap: filter time stddev: 5615574.120183522 | |
val scalaAvg: Double = 1.41805124519E7 | |
val scalaVariance: Double = 2.3432521810268447E14 | |
val scalaStdDev: Double = 1.5307684936092867E7 | |
scala.collection.parallel.mutable.ParHashMap: average filter time: 1.41805124519E7 | |
scala.collection.parallel.mutable.ParHashMap: min filter time: 2373333 | |
scala.collection.parallel.mutable.ParHashMap: max filter time: 417851791 | |
scala.collection.parallel.mutable.ParHashMap: filter time variance: 2.3432521810268447E14 | |
scala.collection.parallel.mutable.ParHashMap: filter time stddev: 1.5307684936092867E7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.System.nanoTime | |
import java.util.concurrent.ConcurrentHashMap | |
import scala.collection.mutable | |
import scala.collection.parallel.mutable.{ParArray, ParHashMap} | |
import scala.jdk.CollectionConverters.ConcurrentMapHasAsScala | |
val javaMap: collection.concurrent.Map[Long, Long] = new ConcurrentHashMap[Long, Long]().asScala | |
val scalaMap = new ParHashMap[Long, Long]() | |
val testSize = 1200000 | |
val iterations = 100 | |
(0 until testSize) foreach(i => { | |
javaMap.put(i, i) | |
scalaMap.put(i, i) | |
}) | |
val javaDurations = mutable.ListBuffer.empty[Long] | |
val scalaDurations = mutable.ListBuffer.empty[Long] | |
0 until iterations foreach { _ => | |
val innerJavaMap = javaMap.clone() | |
val innerScalaMap = scalaMap.clone() | |
0 until iterations foreach { i => | |
val s = nanoTime() | |
innerJavaMap.filterInPlace((k, v) => v > i) | |
javaDurations.addOne(nanoTime() - s) | |
} | |
0 until iterations foreach { i => | |
val s = nanoTime() | |
innerScalaMap | |
.filter { case (key, value) => value <= i } | |
.foreach { case (key, value) => | |
innerScalaMap.remove(key) | |
} | |
scalaDurations.addOne(nanoTime() - s) | |
} | |
} | |
val javaAvg = javaDurations.sum / javaDurations.length.toDouble | |
val javaVariance = javaDurations.map(d => scala.math.pow(d - javaAvg, 2)).sum / javaDurations.length | |
val javaStdDev = scala.math.sqrt(javaVariance) | |
println(s"java.util.concurrent.ConcurrentHashMap: average filter time: $javaAvg") | |
println(s"java.util.concurrent.ConcurrentHashMap: min filter time: ${javaDurations.min}") | |
println(s"java.util.concurrent.ConcurrentHashMap: max filter time: ${javaDurations.max}") | |
println(s"java.util.concurrent.ConcurrentHashMap: filter time variance: $javaVariance") | |
println(s"java.util.concurrent.ConcurrentHashMap: filter time stddev: $javaStdDev") | |
val scalaAvg = scalaDurations.sum / scalaDurations.length.toDouble | |
val scalaVariance = scalaDurations.map(d => scala.math.pow(d - javaAvg, 2)).sum / scalaDurations.length | |
val scalaStdDev = scala.math.sqrt(scalaVariance) | |
println(s"scala.collection.parallel.mutable.ParHashMap: average filter time: $scalaAvg") | |
println(s"scala.collection.parallel.mutable.ParHashMap: min filter time: ${scalaDurations.min}") | |
println(s"scala.collection.parallel.mutable.ParHashMap: max filter time: ${scalaDurations.max}") | |
println(s"scala.collection.parallel.mutable.ParHashMap: filter time variance: $scalaVariance") | |
println(s"scala.collection.parallel.mutable.ParHashMap: filter time stddev: $scalaStdDev") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment