Created
October 4, 2025 03:08
-
-
Save anandwana001/6f308d49296fed9c602c04e33f0c5a6d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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