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

相关推荐
移动开发者1号3 分钟前
Android 多 BaseUrl 动态切换策略(结合 ServiceManager 实现)
android·kotlin
移动开发者1号5 分钟前
Kotlin实现文件上传进度监听:RequestBody封装详解
android·kotlin
alexhilton4 小时前
使用用例(Use Case)以让Android代码更简洁
android·kotlin·android jetpack
zimoyin6 小时前
Java/Kotlin selenium 无头浏览器 [Headless Chrome] 实现长截图 三种方式
java·selenium·kotlin
androidwork8 小时前
Android 中 OkHttp 的自定义 Interceptor 实现统一请求头添加
android·java·okhttp·kotlin
雨白1 天前
高阶函数的应用:简化 SharedPreferences 与 ContentValues 操作
kotlin
移动开发者1号1 天前
Retrofit动态URL与Path参数处理
android·kotlin
移动开发者1号1 天前
Android 中 OkHttp 的自定义 Interceptor 实现统一请求头添加
android·kotlin
小金子同志1 天前
发现 Kotlin MultiPlatform 的一点小变化
kotlin
androidwork1 天前
嵌套滚动交互处理总结
android·java·kotlin