Kotlin性能:runCatching、try-catch块、无异常处理耗时比较
fun main(args: Array<String>) {
val a = 1.1f
val b = 2.2f
val n1 = System.nanoTime()
val m1 = System.currentTimeMillis()
runCatching {
cal(a, b)
}
val n2 = System.nanoTime()
val m2 = System.currentTimeMillis()
try {
cal(a, b)
} catch (e: Exception) {
}
val n3 = System.nanoTime()
val m3 = System.currentTimeMillis()
cal(a, b) //正常的无异常处理流程耗时。
val n4 = System.nanoTime()
val m4 = System.currentTimeMillis()
println("${n2 - n1}ns/${m2 - m1}ms ${n3 - n2}ns/${m3 - m2}ms ${n4 - n3}ns/${m4 - m3}ms")
}
fun cal(a: Float, b: Float) {
a / b
}
1207000ns/1ms 800ns/0ms 100ns/0ms
Kotlin runCatching try-catch耗时比较-CSDN博客文章浏览阅读1k次,点赞26次,收藏20次。文章浏览阅读93次。kotlin异常处理try-catch-finally_zhangphil的博客-CSDN博客。kotlin异常处理try-catch-finally_zhangphil的博客-CSDN博客。kotlin异常处理try-catch-finally_zhangphil的博客-CSDN博客。kotlin异常处理try-catch-finally_zhangphil的博客-CSDN博客。kotlin异常处理try-catch-finally_zhangphil的博客-CSDN博客。https://blog.csdn.net/zhangphil/article/details/140297627