01/08/2019, 2:05 PM
Any suggestions / guidance on how I might optimise this? The arrays contain ~600 elements and this is executed every 500ms to formulate this string which then gets written to a file. Cpu is averaging ~30%.
fun SamplingEvent.DataEvent.SensorArrayValuesEvent.toSensorDataString(): String {
    var segment = ""
    for (i in 0..(sensorCount - 1)) {
        segment += "$timestampNanos,${sensorType[i]},${values0[i]},${values1[i]},${values2[i]},${values3[i]},${values4[i]},${values5[i]}\n"
    return segment.dropLast(1)
My first instinct is to lift this out of extension function and use/reuse StringBuilder (with sufficient initial buffer) ?

Alan Evans

01/08/2019, 2:15 PM
FYI, you can use `until`:
for (i in 0 until sensorCount) {
👍 1
you're not seeing a hint because you have unnecessary brackets. I have raised an issue for that
👍 2