https://kotlinlang.org logo
a

aipok

02/14/2020, 1:53 PM
how can I improve speed of
Copy code
(min..max).toList
it takes 5000 .. 50000 -> 6.516391 s to generate
s

spand

02/14/2020, 1:56 PM
Cache the result? Use the IntRange directly?
j

jw

02/14/2020, 1:57 PM
Or implement an IntRange-backed List yourself since the getter is just min + index
a

aipok

02/14/2020, 1:59 PM
Thanks @jw
I don’t need to cache the result, since it is one time operation, but since it is during the component initialization. It would be nice to do it faster. Initially the range was small, but now it turned out that existing approach doesn’t work well 😄
a

arekolek

02/14/2020, 2:05 PM
this is somewhat faster
Copy code
measureNanoTime {
    val list = (5000..50000).toList()
}

measureNanoTime {
    val list = (5000..50000).let { range ->
        List(range.last - range.first + 1) { range.first + it }
    }
}
a

aipok

02/14/2020, 2:07 PM
yah, it is. Thanks for advise
k

Kroppeb

02/14/2020, 8:20 PM
I'm mostly wondering why you need such a list
2 Views