Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- val listA = List(10_000) { Random.nextInt() }
- val listB = List(10_000) { Random.nextInt() }
- open class MapBenchmarks {
- @Benchmark
- fun manual(bh: Blackhole) {
- val result = mutableMapOf<Int, Int>()
- for (i in listA.indices)
- result[listA[i]] = listB[i]
- bh.consume(result.size)
- }
- @Benchmark
- fun manualAlloc(bh: Blackhole) {
- val result = LinkedHashMap<Int, Int>(listA.size)
- for (i in listA.indices)
- result[listA[i]] = listB[i]
- bh.consume(result.size)
- }
- @Benchmark
- fun zip(bh: Blackhole) {
- val result = listA.zip(listB).toMap()
- bh.consume(result.size)
- }
- @Benchmark
- fun sequenceZip(bh: Blackhole) {
- val result = listA.asSequence().zip(listB.asSequence()).toMap()
- bh.consume(result.size)
- }
- @Benchmark
- fun withIndexAssociate(bh: Blackhole) {
- val result = listA.withIndex().associate { (i, v) -> v to listB[i] }
- bh.consume(result.size)
- }
- @Benchmark
- fun withIndexAssociateByTo(bh: Blackhole) {
- //from Karel
- val result = listA.withIndex().associateBy( {(_, v) -> v}, { (i, _) -> listB[i]} )
- bh.consume(result.size)
- }
- @Benchmark
- fun iterator(bh: Blackhole) {
- //from Arkady
- val iterB = listB.iterator()
- val result = listA.associateWith { iterB.next() }
- bh.consume(result.size)
- }
- @Benchmark
- fun mapIndexed(bh: Blackhole) {
- // from Trevor
- val result = listA.mapIndexed { index, value -> value to listB[index] }.toMap()
- bh.consume(result.size)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement