Created
February 22, 2023 02:39
-
-
Save Bruce0203/0502ad60b4ae69b5739d9c45e53c9884 to your computer and use it in GitHub Desktop.
This file contains 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 java.util.UUID | |
import kotlin.system.measureTimeMillis | |
fun main() { | |
fun newRandomString() = UUID.randomUUID().toString() | |
val list = (0..100).map { newRandomString() }.toList() | |
val times = 100000 | |
val a = measureTimeMillis { | |
repeat(times) { | |
list.forEach { } | |
} | |
} | |
val b = measureTimeMillis { | |
repeat(times) { | |
list.fastForEach { } | |
} | |
} | |
println("a=$a, b=$b") | |
} | |
inline fun <T> List<T>.fastForEach(callback: (T) -> Unit) { | |
var n = 0 | |
while (n < size) callback(this[n++]) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment