统计方法耗时
2022-11-02 本文已影响0人
ModestStorm
方式1
fun testCostTime(){
var list = listOf<Int>(1000)
Thread.sleep(1000)
}
// 耗时打印
val timeCost = measureTimeMillis {
testCostTime()
}
println("the code run time is 耗时:$timeCost")
方式2
//不使用Sequences序列,使用普通的集合操作
fun computeRunTime(action: (() -> Unit)?) {
val startTime = System.currentTimeMillis()
action?.invoke()
println("the code run time is ${System.currentTimeMillis() - startTime}")
}
// 方式2
computeRunTime {
testCostTime()
}