Vladimir Vainer
01/12/2023, 8:46 AMimport kotlin.math.PI
import kotlin.system.measureTimeMillis
fun test1(a : FloatArray) : Float {
val angles = a.map { it.toDegrees() }
val yaw = angles[0]
val pitch = angles[1]
return yaw + pitch
}
fun test2(angles : FloatArray) : Float {
val yaw = angles[0].toDegrees()
val pitch = angles[1].toDegrees()
return yaw + pitch
}
fun Float.toDegrees() : Float {
return (this * 180 / PI).toFloat()
}
fun main(args: Array<String>) {
val a = floatArrayOf(0.5f, 0.7f)
val test2Time = measureTimeMillis {
var out = 0L
for (i in 0..1000000) {
out += test2(a).toLong()
}
}
val test1Time = measureTimeMillis {
var out = 0L
for (i in 0..1000000) {
out += test1(a).toLong()
}
}
println("Test1: $test1Time ms")
println("Test2: $test2Time ms")
}
Test1: 42 ms
Test2: 7 ms
Using map is pretty costy. Am i doing something wrong?Sam
01/12/2023, 8:50 AMVladimir Vainer
01/12/2023, 8:53 AMephemient
01/12/2023, 8:59 AMSam
01/12/2023, 8:59 AM