https://kotlinlang.org logo
#getting-started
Title
# getting-started
i

Ido Flax

03/16/2022, 6:46 AM
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

gildor

03/16/2022, 7:27 AM
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

Ido Flax

03/16/2022, 7:34 AM
Ok, interesting, thanks
j

Joffrey

03/16/2022, 7:43 AM
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

gildor

03/16/2022, 7:46 AM
Yep, good point, Default/IO also will creates all thread pools on first access
i

Ido Flax

03/16/2022, 7:50 AM
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

gildor

03/16/2022, 7:53 AM
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)