Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. val listA = List(10_000) { Random.nextInt() }
  2. val listB = List(10_000) { Random.nextInt() }
  3.  
  4. open class MapBenchmarks {
  5. @Benchmark
  6. fun manual(bh: Blackhole) {
  7. val result = mutableMapOf<Int, Int>()
  8. for (i in listA.indices)
  9. result[listA[i]] = listB[i]
  10.  
  11. bh.consume(result.size)
  12. }
  13.  
  14. @Benchmark
  15. fun manualAlloc(bh: Blackhole) {
  16. val result = LinkedHashMap<Int, Int>(listA.size)
  17. for (i in listA.indices)
  18. result[listA[i]] = listB[i]
  19.  
  20. bh.consume(result.size)
  21. }
  22.  
  23. @Benchmark
  24. fun zip(bh: Blackhole) {
  25. val result = listA.zip(listB).toMap()
  26. bh.consume(result.size)
  27. }
  28.  
  29. @Benchmark
  30. fun sequenceZip(bh: Blackhole) {
  31. val result = listA.asSequence().zip(listB.asSequence()).toMap()
  32. bh.consume(result.size)
  33. }
  34.  
  35. @Benchmark
  36. fun withIndexAssociate(bh: Blackhole) {
  37. val result = listA.withIndex().associate { (i, v) -> v to listB[i] }
  38. bh.consume(result.size)
  39. }
  40.  
  41. @Benchmark
  42. fun withIndexAssociateByTo(bh: Blackhole) {
  43. //from Karel
  44. val result = listA.withIndex().associateBy( {(_, v) -> v}, { (i, _) -> listB[i]} )
  45. bh.consume(result.size)
  46. }
  47.  
  48. @Benchmark
  49. fun iterator(bh: Blackhole) {
  50. //from Arkady
  51. val iterB = listB.iterator()
  52. val result = listA.associateWith { iterB.next() }
  53. bh.consume(result.size)
  54. }
  55.  
  56. @Benchmark
  57. fun mapIndexed(bh: Blackhole) {
  58. // from Trevor
  59. val result = listA.mapIndexed { index, value -> value to listB[index] }.toMap()
  60. bh.consume(result.size)
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement