I noticed that ```CoroutineScope.launch{ }``` take...
# getting-started
i
I noticed that
Copy code
CoroutineScope.launch{ }
takes 40 ms. Isn’t this a bit much? Part of the point of launching is that you don’t need to wait for it, but apparently you do need to wait 40 ms as least
Copy code
val scope = CoroutineScope(Dispatchers.Default)
    println(
        measureTimeMillis {
            scope.launch { }
        }
    )
// 40
This is done on a macbook pro 2021 m1 pro
😲 1
g
Only on first launch? Maybe it’s just class loader penalty on first access
Yep, looks like classloader/bytecode optimization penalty on first lauch, I have 42/5/0/0/1/0/etc results per launch
i
Ok, interesting, thanks
j
Also maybe the first use of
Dispatchers.Default
actually creates the threads in that pool
In any case, don't trust your microbenchmark if you don't use JMH :)
g
Yep, good point, Default/IO also will creates all thread pools on first access
i
Copy code
val scope = CoroutineScope(newFixedThreadPoolContext(1, "test"))
println(
    measureTimeMillis {
        scope.launch { }
    }
)
println(
    measureTimeMillis {
        scope.launch { }
    }
)
println(
    measureTimeMillis {
        scope.launch { }
    }
)
44 22 0
anyway, i guess all good then 🙂 thanks
g
But anyway, hard to say what takes this time without profiling, it worth to do if it’s a problem (for example there are many Dispatcher.Main for Android improvements, which optimizes first access)