Skip to content

Instantly share code, notes, and snippets.

@Bruce0203
Created February 22, 2023 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bruce0203/0502ad60b4ae69b5739d9c45e53c9884 to your computer and use it in GitHub Desktop.
Save Bruce0203/0502ad60b4ae69b5739d9c45e53c9884 to your computer and use it in GitHub Desktop.
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