Trey
02/15/2024, 9:25 PMAdam S
02/15/2024, 9:46 PMTrey
02/15/2024, 9:47 PMval buffer = _ogg_sync_buffer_(syncState, bufferSize)
Miroslav Sobotka
02/16/2024, 8:16 AMMiroslav Sobotka
02/16/2024, 9:01 AMmemcpy
and got interessting numbers, huge difference between debug and release:
> ./debugExecutable/memcpy-test.kexe
# STDLIB
Stressing with 1000 MiB of memory...
Allocated 1000 MiB in 3.447955416s
AddressOf took 2.375us
Read bytes took 33.738578875s
Array size: 1048576000
First 10 elements: [0 1 2 3 4 5 6 7 8 9]
# MEMCPY
Stressing with 1000 MiB of memory...
Allocated 1000 MiB in 3.466606875s
AddressOf took 2.75us
Read bytes took 779.767958ms
Array size: 1048576000
First 10 elements: [0 1 2 3 4 5 6 7 8 9]
> ./releaseExecutable/memcpy-test.kexe
# STDLIB
Stressing with 1000 MiB of memory...
Allocated 1000 MiB in 580.001167ms
AddressOf took 625ns
Read bytes took 755.114958ms
Array size: 1048576000
First 10 elements: [0 1 2 3 4 5 6 7 8 9]
# MEMCPY
Stressing with 1000 MiB of memory...
Allocated 1000 MiB in 573.892666ms
AddressOf took 750ns
Read bytes took 148.824833ms
Array size: 1048576000
First 10 elements: [0 1 2 3 4 5 6 7 8 9]
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.addressOf
import kotlinx.cinterop.pin
import kotlinx.cinterop.readBytes
import platform.posix.memcpy
import kotlin.time.measureTimedValue
fun main() {
println("# STDLIB")
stressStdlib(1000).printFirstBytes(10)
println("# MEMCPY")
stressMemcpy(1000).printFirstBytes(10)
}
fun ByteArray.printFirstBytes(count: Int = 10) {
println("Array size: $size")
println("First 10 elements: ${take(count).joinToString(separator = " ", prefix = "[", postfix = "]")}")
}
@OptIn(ExperimentalForeignApi::class)
fun stressStdlib(sizeInMegabytes: Int = 100): ByteArray {
println("Stressing with $sizeInMegabytes MiB of memory...")
val sizeInBytes = sizeInMegabytes * 1024 * 1024
val (byteArray, timeByteArray) = measureTimedValue {
ByteArray(sizeInBytes) { it.toByte() }
}
println("Allocated $sizeInMegabytes MiB in $timeByteArray")
val (addressOf, timeAddressOf) = measureTimedValue { byteArray.pin().addressOf(0) }
println("AddressOf took $timeAddressOf")
val (array, timeReadBytes) = measureTimedValue {
addressOf.readBytes(sizeInBytes)
}
println("Read bytes took $timeReadBytes")
return array
}
@OptIn(ExperimentalForeignApi::class)
fun stressMemcpy(sizeInMegabytes: Int = 100): ByteArray {
println("Stressing with $sizeInMegabytes MiB of memory...")
val sizeInBytes = sizeInMegabytes * 1024 * 1024
val (byteArray, timeByteArray) = measureTimedValue {
ByteArray(sizeInBytes) { it.toByte() }
}
println("Allocated $sizeInMegabytes MiB in $timeByteArray")
val (addressOf, timeAddressOf) = measureTimedValue { byteArray.pin().addressOf(0) }
println("AddressOf took $timeAddressOf")
val (array, timeReadBytes) = measureTimedValue {
val result = ByteArray(sizeInBytes)
memcpy(result.pin().addressOf(0), addressOf, sizeInBytes.toULong())
result
}
println("Read bytes took $timeReadBytes")
return array
}
Chanjung Kim
02/18/2024, 4:30 AM