Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. package org.openjdk.jmh.samples
  2.  
  3. import org.openjdk.jmh.annotations._
  4.  
  5. import scala.collection.{mutable, immutable}
  6. /*
  7. Scala immutable map is ~9x slower than java Hashmap for lookups.
  8. Scala mutable map is ~2x slower than java Hashmap for lookups.
  9. Benchmark results:
  10. Scala immutable map
  11. [info] ToBenchmark.imapBench thrpt 3 45984712.982 ± 70602214.006 ops/s
  12. Java Hashmap
  13. [info] ToBenchmark.jmapBench thrpt 3 420077614.268 ± 137667083.271 ops/s
  14. Scala mutable map
  15. [info] ToBenchmark.mmapBench thrpt 3 232789150.801 ± 93773395.056 ops/s
  16. */
  17. @Fork(1)
  18. @Warmup(iterations = 3)
  19. @Measurement(iterations = 3)
  20. @State(Scope.Benchmark)
  21. class ToBenchmark {
  22. val imap = (1 to 100000).map{ x => x -> x }.toMap
  23. val mmap = {
  24. val m = mutable.Map[Int, Int]()
  25. for (i <- 1 to 10000) m += i -> i
  26. m
  27. }
  28.  
  29. val jmap = {
  30. val hm = new java.util.HashMap[Int, Int]()
  31. for (i <- 1 to 10000) hm.put(i, i)
  32. hm
  33. }
  34.  
  35. @Benchmark
  36. def imapBench(): Unit = {
  37. imap(1)
  38. }
  39.  
  40. @Benchmark
  41. def mmapBench(): Unit = {
  42. mmap(1)
  43. }
  44.  
  45. @Benchmark
  46. def jmapBench(): Unit = {
  47. jmap.get(1)
  48. }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement