K/N IOS - IntArray Performance Issue

Hello Kotlin Friends,

I do have issues with the performance of Kotliin/Native IntArray access.
Here is the code:

   fun createGetSet() {

        val time = measureTime {
            for (i in 0..loopNumber) {
                val array = IntArray(arraySize) { index -> index }
                for (index in 0 until arraySize) {
                    val value = array[index]
                    array[index] = value * 2
        println("[Int Array] CreateGetSet: ${time.inWholeMilliseconds} ms")

With a loop number of 1000 and an arraySize of 500x500, the measured time is:

on JVM - [Int Array] CreateGetSet: 154 ms
on iOSX64 - [Int Array] CreateGetSet: 3385 ms

This seems to be related to bridging Kotlin Int with native primitive int.
Any ideas how to solve iOS PERFORMANCE ???


You should probably pass resulting arrays somewhere or better, use JMH for measuring the performance in JVM. JVM performs a lot of optimizations while your code runs. It is possible this code is entirely removed after JVM notices it does really nothing.

Thanks broot,

But my actual concern is doing operation on array element. And these operation are low on iOS. JVM is ok for now.

I understand that the performance on iOS is what concerns you, but how do you know it is really slow? 250e6 operations is quite a lot, so maybe ~3s is actually fast.

One thing that may cause Kotlin array accesses slower than in languages like e.g. C are boundary checks. I don’t use Koltin/Native, so I may be wrong, but I guess it adds such checks.

I suspect that mapping Kotlin Int to iOS Int is the problem !!!
Maybe boxing - unboxing !