Skip to content

Instantly share code, notes, and snippets.

@Bruce0203
Last active March 13, 2023 09:30
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/368e2089e273bd7bb2effa48975f3454 to your computer and use it in GitHub Desktop.
Save Bruce0203/368e2089e273bd7bb2effa48975f3454 to your computer and use it in GitHub Desktop.
performance optimized forEach in Kotlin (exclude LinkedList case)
inline fun <T> List<T>.fastForEach(callback: (T) -> Unit) {
var n = 0
while (n < size) callback(this[n++])
}
inline fun <T> Array<T>.fastForEach(callback: (T) -> Unit) {
var n = 0
while (n < size) callback(this[n++])
}
@yejunho10
Copy link

wtf is this??

@Bruce0203
Copy link
Author

Bruce0203 commented Mar 3, 2023

arm에서는 최대 3배, x86에서는 1.1배 더 빠른 forEach 최적화입니다 @yejunho10

@Bruce0203
Copy link
Author

153DDE50-B5F6-4FBF-8E2E-D8A9243EF04C
jmh 통해서 구했는데 warmup 하고 저는 이렇게 나오네요

@idkNicks
Copy link

저거 위에 링크 답변좀 부탁드릴게요!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment