Skip to content

Instantly share code, notes, and snippets.

@anandwana001
Created October 4, 2025 03:08
Show Gist options
  • Select an option

  • Save anandwana001/6f308d49296fed9c602c04e33f0c5a6d to your computer and use it in GitHub Desktop.

Select an option

Save anandwana001/6f308d49296fed9c602c04e33f0c5a6d to your computer and use it in GitHub Desktop.
import kotlinx.coroutines.*
import kotlin.system.measureTimeMillis
suspend fun performanceTest() {
val data = List(1000) { it }
// Sequential processing
val sequentialTime = measureTimeMillis {
data.forEach { item ->
delay(10)
// process item
}
}
println("Sequential: $sequentialTime ms")
// Parallel with Default dispatcher
val parallelDefaultTime = measureTimeMillis {
coroutineScope {
data.map { item ->
async(Dispatchers.Default) {
delay(10)
// process item
}
}.awaitAll()
}
}
println("Parallel (Default): $parallelDefaultTime ms")
// Parallel with IO dispatcher
val parallelIOTime = measureTimeMillis {
coroutineScope {
data.map { item ->
async(Dispatchers.IO) {
delay(10)
// process item
}
}.awaitAll()
}
}
println("Parallel (IO): $parallelIOTime ms")
}
fun main() = runBlocking {
performanceTest()
}
/* Expected output:
Sequential: ~10000 ms
Parallel (Default): ~1250 ms (limited by CPU cores)
Parallel (IO): ~200 ms (can have 64 concurrent operations)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment