In terms of performance, is it better to work with...
# kotlin-native
m
In terms of performance, is it better to work with a FloatArray or an allocated CArrayPointer<FloatVar>, or is it about equal? edit: did some testing in this thread, tl;dr is that in release binaries K/N is slightly slower than c++ with no optimization flags but still fast enough for realtime tasks
👌 1
Did a quick test:
Copy code
native array | write: 37ms
native array | read: 36ms
native array | copy: 71ms
native array | memcpy: 0ms
Kotlin array | write: 4ms
Kotlin array | read: 2ms
Kotlin array | write/read: 6ms
Kotlin array | copyInto: 0ms
Code if anyone else wants to run the tests:
Copy code
import kotlinx.cinterop.*
import platform.posix.CLOCKS_PER_SEC
import platform.posix.clock
import platform.posix.memcpy
import kotlin.random.Random

fun test(name: String, block: () -> Unit) {
    val start = clock()
    block()
    val end = clock()
    val elapsed = end - start
    println("$name: ${elapsed * 1000 / CLOCKS_PER_SEC}ms")
}

fun main() {
    val size = 1_000_000
    val rand = Random(clock())
    val arr1 = FloatArray(size) { rand.nextFloat() }
    val arr2 = FloatArray(size) { rand.nextFloat() }
    val nat1 = nativeHeap.allocArray<FloatVar>(size) { rand.nextFloat() }
    val nat2 = nativeHeap.allocArray<FloatVar>(size) { rand.nextFloat() }

    test("native array | write") {
        for (i in 0 until size) {
            nat2[i] = i.toFloat()
        }
    }

    test("native array | read") {
        for (i in 0 until size) {
            val x = nat1[i]
        }
    }

    test("native array | copy") {
        for (i in 0 until size) {
            nat2[i] = nat1[i]
        }
    }

    test("native array | memcpy") {
        memcpy(nat2, nat1, (size * sizeOf<FloatVar>()).convert())
    }

    test("Kotlin array | write") {
        for (i in 0 until size) {
            arr2[i] = i.toFloat()
        }
    }

    test("Kotlin array | read") {
        for (i in 0 until size) {
            val x = arr1[i]
        }
    }

    test("Kotlin array | write/read") {
        for (i in 0 until size) {
            arr2[i] = arr1[i]
        }
    }

    test("Kotlin array | copyInto") {
        arr1.copyInto(arr2)
    }

    nativeHeap.free(nat1)
    nativeHeap.free(nat2)
}
Update after running release from kotlin native:
Copy code
native array | write: 1595μs
native array | read: 252μs
native array | copy: 1181μs
native array | memcpy: 473μs
Kotlin array | write: 2120μs
Kotlin array | read: 0μs
Kotlin array | write/read: 2168μs
Kotlin array | copyInto: 513μs

-- written in cpp and compiled with gcc, no flags --
cpp array | write: 1550μs
cpp array | read: 1909μs
cpp array | write/read: 1934μs
cpp array | memcpy: 562μs
Aside from reads being unreliable due to the compiler optimizing them out, this is pretty good performance from K/N all things considered
👌 1