4/30/2023 0 Comments Mapof kotlin![]() TreeHashMap is useful in certain scenarios for example. They are not the only implementation of Map of course. inline fun Map .zip(other: Map , map: (Key, A.Definitely use mapOf, unless you need any specific behavior of the map iterators, like SortedMap, or HashMap offer. mapOf pays a tiniest bit extra price to keep it and give you the least surprise. Like java, it can be of the same type and feature. The only difference is that hashMapOf does not preserve the order of elements - they will be shuffled during iteration. ![]() ![]() I wonder if mentioning the performance win for HashMap() vs. Results in > compareMapInits for 10 elements (repeated 1 times) - mutableMapOf: time taken: 3ms - HashMap: time taken: 0ms > compareMapInits for 100 elements (repeated 1 times) - mutableMapOf: time taken: 0ms - HashMap: time taken: 0ms > compareMapInits for 10000 elements (repeated 1 times) - mutableMapOf: time taken: 2ms - HashMap: time taken: 1ms > compareMapInits for 1000000 elements (repeated 1 times) - mutableMapOf: time taken: 107ms - HashMap: time taken: 119ms > compareMapInits for 10 elements (repeated 1000 times) mutableMapOf() total time taken: 5 ms HashMap() total time taken: 2 ms How often is HashMap() slower? 2/1000 > compareMapInits for 100 elements (repeated 1000 times) mutableMapOf() total time taken: 8 ms HashMap() total time taken: 10 ms How often is HashMap() slower? 10/1000 > compareMapInits for 10000 elements (repeated 1000 times) mutableMapOf() total time taken: 335 ms HashMap() total time taken: 225 ms How often is HashMap() slower? 206/1000 > compareMapInits for 1000000 elements (repeated 1000 times) mutableMapOf() total time taken: 81266 ms HashMap() total time taken: 69712 ms How often is HashMap() slower? 180/1000 can be a great choice IMO, as it explicitly states the importance of ordering here. Recently, when doing a small coding challenge in Kotlin I was faced with the choice of mutableMapOf(). Function we use to initialize Maps One time run fun main() Kotlin mapOf and mutableMapOf Performance Trade-off.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |