Skip to content

Instantly share code, notes, and snippets.

@taku0
Created December 6, 2011 14:13
Show Gist options
  • Save taku0/1438338 to your computer and use it in GitHub Desktop.
Save taku0/1438338 to your computer and use it in GitHub Desktop.
scala.collection.mutable.HashSet遅い?
import scala.collection.JavaConverters._
object Main extends App {
def bench(set1: java.util.Set[Int], set2: java.util.Set[Int]) = {
val start = System.nanoTime
var sum = 0
0.until(10).map(_ => {
val diff = new java.util.HashSet[Int]
diff.addAll(set1)
diff.removeAll(set2)
diff.size
})
val end = System.nanoTime
println("Java Set: %s ms".format((end - start) / 1000.0 / 1000.0))
}
def bench(set1: scala.collection.mutable.Set[Int], set2: scala.collection.mutable.Set[Int]) = {
val start = System.nanoTime
var sum = 0
0.until(10).map(_ => {
val diff = scala.collection.mutable.HashSet.empty[Int]
diff ++= set1
// ++= が遅いというのを示すためにここはコメントアウトしておく
// diff --= set2
diff.size
})
val end = System.nanoTime
println("Scala Set: %s ms".format((end - start) / 1000.0 / 1000.0))
}
val values1 = 0.until(100000).filter(_ => math.random < 0.5)
val values2 = 0.until(100000).filter(_ => math.random < 0.5)
val javaSet1 = new java.util.HashSet[Int]
val javaSet2 = new java.util.HashSet[Int]
javaSet1.addAll(values1.asJava)
javaSet2.addAll(values2.asJava)
val scalaSet1 = scala.collection.mutable.HashSet.empty[Int]
val scalaSet2 = scala.collection.mutable.HashSet.empty[Int]
scalaSet1 ++= values1
scalaSet2 ++= values2
for (x <- 0.until(100)) {
bench(javaSet1, javaSet2)
bench(scalaSet1, scalaSet2)
bench(javaSet1, javaSet2)
bench(scalaSet1, scalaSet2)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment