Jetpack Compose mutableStateList 与 mutableList 性能对比

Jetpack Compose mutableStateList vs mutableList Performance

与普通变量、mutableList、mutableMap 相比,mutableState、mutableStateList、mutableStateMap 表现如何?如果有观察者监听他们当然会慢一些(因为触发了重组)但是如果根本没有观察者他们之间有区别吗?

提前致谢。

我认为这里用词“慢”不合适,但如果有任何可组合项正在观察该状态变量 - 它将被重新组合。因此,如果执行得太频繁,它可能会减慢您的应用程序。

你无法真正比​​较它们,因为它们的用途完全不同。 mutableState 和其他人在幕后依赖标准的 kotlin 结构。它只是为 compose kotlin 编译器插件添加了要监视的“可观察”部分。

最后我写了一个简单的class来测试性能:

import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf


fun main() {
    val numbers = 1000000
    for (i in 1 until 11) {
        println("test number $i")

        testNormalVariable(numbers)
        testStateVariable(numbers)
        testNormalList(numbers)
        testStateList(numbers)

        println()
    }
}

fun testNormalVariable(numbers: Int) {
    var normalVariable = 0
    val startTime = System.currentTimeMillis()
    for (i in 0 until numbers) {
        normalVariable = i
    }
    val time = System.currentTimeMillis() - startTime
    println("testNormalVariable took $time ms")
}

fun testStateVariable(numbers: Int) {
    val stateVariable = mutableStateOf(0)
    val startTime = System.currentTimeMillis()
    for (i in 0 until numbers) {
        stateVariable.value = i
    }
    val time = System.currentTimeMillis() - startTime
    println("testStateVariable took $time ms")
}

fun testNormalList(numbers: Int) {
    val normalList = mutableListOf<Int>()
    val startTime = System.currentTimeMillis()
    for (i in 0 until numbers) {
        normalList.add(i)
    }
    val time = System.currentTimeMillis() - startTime
    println("testNormalList took $time ms")
}

fun testStateList(numbers: Int) {
    val stateList = mutableStateListOf<Int>()
    val startTime = System.currentTimeMillis()
    for (i in 0 until numbers) {
        stateList.add(i)
    }
    val time = System.currentTimeMillis() - startTime
    println("testStateList took $time ms")
}

很明显,mutableState 和 mutableStateList 比普通变量或 mutableList 慢得多:

test number 1
testNormalVariable took 5 ms
testStateVariable took 82 ms
testNormalList took 48 ms
testStateList took 366 ms

test number 2
testNormalVariable took 5 ms
testStateVariable took 94 ms
testNormalList took 121 ms
testStateList took 368 ms

test number 3
testNormalVariable took 0 ms
testStateVariable took 35 ms
testNormalList took 30 ms
testStateList took 319 ms

test number 4
testNormalVariable took 0 ms
testStateVariable took 27 ms
testNormalList took 25 ms
testStateList took 299 ms

test number 5
testNormalVariable took 0 ms
testStateVariable took 32 ms
testNormalList took 29 ms
testStateList took 289 ms

test number 6
testNormalVariable took 0 ms
testStateVariable took 26 ms
testNormalList took 20 ms
testStateList took 310 ms

test number 7
testNormalVariable took 0 ms
testStateVariable took 41 ms
testNormalList took 30 ms
testStateList took 413 ms

test number 8
testNormalVariable took 0 ms
testStateVariable took 25 ms
testNormalList took 28 ms
testStateList took 142 ms

test number 9
testNormalVariable took 0 ms
testStateVariable took 93 ms
testNormalList took 29 ms
testStateList took 298 ms

test number 10
testNormalVariable took 0 ms
testStateVariable took 46 ms
testNormalList took 32 ms
testStateList took 309 ms