Kotlin runCatching try-catch耗时比较

Kotlin runCatching try-catch耗时比较

Kotlin 复制代码
fun main(args: Array<String>) {
    val lists = arrayListOf("z")

    val idx = 10

    /**
     *纳秒统计
     *
     * ns(nanosecond):纳秒。一秒的10亿分之一,10的-9次方秒。
     *   1纳秒=0.000001 毫秒
     *   1纳秒=0.000000001秒
     */

    val t1 = System.nanoTime()
    kotlin.runCatching {
        lists[idx]
    }.onFailure {
        //println("error1")
    }

    val t2 = System.nanoTime()

    try {
        lists[idx]
    } catch (e: Exception) {
        //println("error2")
    }
    val t3 = System.nanoTime()

    println("${t2 - t1}ns ${t3 - t2}ns ${(t2 - t1) / (t3 - t2)}")
}

8493500ns 129600ns 65

Kotlin异常处理runCatching,getOrDefault,getOrNull run(2)_kotlin.runcatchin getorthrow 返回值小于等于0-CSDN博客文章浏览阅读215次。b-catch: java.lang.RuntimeException: b发生异常。kotlin异常处理try-catch-finally。kotlin异常处理try-catch-finally_zhangphil的博客-CSDN博客。b-catch: java.lang.RuntimeException: b发生异常。kotlin异常处理try-catch-finally。kotlin异常处理try-catch-finally_zhangphil的博客-CSDN博客。_kotlin.runcatchin getorthrow 返回值小于等于0https://blog.csdn.net/zhangphil/article/details/133279933runCatching异常捕获onSuccess/onFailure返回函数,Kotlin_runcatching 函数finally-CSDN博客文章浏览阅读717次,点赞8次,收藏8次。文章浏览阅读93次。kotlin异常处理try-catch-finally。kotlin异常处理try-catch-finally_zhangphil的博客-CSDN博客。kotlin异常处理try-catch-finally。kotlin异常处理try-catch-finally_zhangphil的博客-CSDN博客。文章浏览阅读533次。kotlin异常处理try-catch-finally。kotlin异常处理try-catch-finally_zhangphil的博客-CSDN博客。_runcatching 函数finallyhttps://blog.csdn.net/zhangphil/article/details/135048822

Kotlin异常处理runCatching,getOrNull,onFailure,onSuccess(1)_kotlin runcatching性能问题-CSDN博客文章浏览阅读761次。b-catch: java.lang.RuntimeException: b发生异常。kotlin异常处理try-catch-finally。kotlin异常处理try-catch-finally_zhangphil的博客-CSDN博客。_kotlin runcatching性能问题https://blog.csdn.net/zhangphil/article/details/133279853

相关推荐
用户45989204565164 小时前
一套 KMP 多仓库版本治理工作流:用 Version Catalog + CI 把发版流水线自动化
前端·kotlin
独隅7 小时前
KMP 全栈进化:Koog 框架打造纯 Kotlin AI Agent 实战效果
开发语言·人工智能·kotlin
pengyu1 天前
【Kotlin 协程修仙录 · 元婴境 · 中阶】 | 操作符真解:Flow 中间操作符与数据流变幻术
android·kotlin
pengyu1 天前
【Kotlin 协程修仙录 · 元婴境 · 初阶】 | 冷流初啼:Flow 的基础与响应式编程的入门
android·kotlin
Kapaseker2 天前
破坏性更新 - 解读 Jetpack Compose 1.12
android·kotlin
Android-Flutter2 天前
android LeakCanary 工作原理 详解
android·kotlin
Android-Flutter2 天前
android 自定义view 详解
android·kotlin
Android打工仔2 天前
从 finally 理解程序的控制流:它为什么不是 catch 后面的代码?
android·kotlin
YXL1111YXL2 天前
续体和状态机 —— suspend 函数的 CPS 变换
android·kotlin
alexhilton2 天前
千万别误用Android Skills
android·kotlin·android jetpack