Created
February 6, 2016 21:40
-
-
Save OndrejSpanel/0a8767f0e083875d0989 to your computer and use it in GitHub Desktop.
Test builder vs. update performance for HashMap
This file contains hidden or 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
object HashTest extends App { | |
import scala.collection.generic.CanBuildFrom | |
import scala.collection.immutable.HashMap | |
type HM = HashMap[Int, String] | |
def updated(map: HM, key: Int, value: String) = { | |
map.updated(key, value) | |
} | |
def builder(m: HM, key: Int, value: String)(implicit cbf: CanBuildFrom[HM, (Int, String), HM]) = { | |
val builder = cbf(m) | |
m.foreach { case (k, v) => k -> (if (k == key) value else v)} | |
builder.result | |
} | |
val map = HashMap((0 to 10000).map(p => p -> p.toString): _*) | |
def now = System.currentTimeMillis | |
for (rep <- 0 to 10) { | |
{ | |
val time = now | |
for (i <- 0 to 10000) { | |
updated(map, 500, "xxx") | |
} | |
println(s"Update ${now - time}") | |
} | |
{ | |
val time = now | |
for (i <- 0 to 10000) { | |
builder(map, 500, "xxx") | |
} | |
println(s"Builder ${now - time}") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output on my system is: