Created
May 7, 2016 18:29
-
-
Save ruippeixotog/199e72cfcf26577ff3c07bc16dec4e42 to your computer and use it in GitHub Desktop.
ListMap and ListSet benchmarks - Builder
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
def benchmark[A](desc: String)(block: => A): Option[A] = { | |
val t = System.currentTimeMillis() | |
try { | |
val res = block | |
println(s"$desc: took ${System.currentTimeMillis() - t} ms") | |
Some(res) | |
} catch { | |
case e: Throwable => | |
e.printStackTrace() | |
println(s"$desc: ${e.getClass.getSimpleName} thrown after ${System.currentTimeMillis() - t} ms") | |
None | |
} | |
} | |
val ops = 50000 | |
// warm-up | |
(0 to 10).foreach { n => | |
val elems = (0 until n).map(e => (e, e)) | |
(0 to 5000).foreach(_ => ListMap(elems: _*)) | |
} | |
// run benchmarks | |
((0 to 10) ++ List(20, 50, 100, 200)).foreach { n => | |
val elems = (0 until n).map(e => (e, e)) | |
benchmark(s"Create size $n ListMap") { | |
var i = ops | |
while(i > 0) { ListMap(elems: _*); i -= 1 } | |
} | |
} |
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
Before: | |
Create size 0 ListMap: took 10 ms | |
Create size 1 ListMap: took 8 ms | |
Create size 2 ListMap: took 12 ms | |
Create size 3 ListMap: took 16 ms | |
Create size 4 ListMap: took 16 ms | |
Create size 5 ListMap: took 36 ms | |
Create size 6 ListMap: took 33 ms | |
Create size 7 ListMap: took 37 ms | |
Create size 8 ListMap: took 54 ms | |
Create size 9 ListMap: took 63 ms | |
Create size 10 ListMap: took 93 ms | |
Create size 20 ListMap: took 290 ms | |
Create size 50 ListMap: took 1692 ms | |
Create size 100 ListMap: took 6312 ms | |
Create size 200 ListMap: took 25312 ms | |
After: | |
Create size 0 ListMap: took 18 ms | |
Create size 1 ListMap: took 16 ms | |
Create size 2 ListMap: took 25 ms | |
Create size 3 ListMap: took 22 ms | |
Create size 4 ListMap: took 20 ms | |
Create size 5 ListMap: took 21 ms | |
Create size 6 ListMap: took 25 ms | |
Create size 7 ListMap: took 25 ms | |
Create size 8 ListMap: took 43 ms | |
Create size 9 ListMap: took 48 ms | |
Create size 10 ListMap: took 32 ms | |
Create size 20 ListMap: took 88 ms | |
Create size 50 ListMap: took 256 ms | |
Create size 100 ListMap: took 385 ms | |
Create size 200 ListMap: took 909 ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment