Smallville7123
04/18/2019, 7:55 PMfor
loop is faster than a forEach
expression right?karelpeeters
04/18/2019, 7:56 PMforeach
is often inline
, and then it yields the same exact bytecode.Smallville7123
04/18/2019, 7:58 PMlouiscad
04/18/2019, 8:21 PMforEachByIndex
extension for List
karelpeeters
04/18/2019, 8:23 PMtseisel
04/18/2019, 9:02 PMbreak
or continue
, use the for
loop as it is not supported in forEach
.pablisco
04/18/2019, 9:31 PMforEachIndexed
is not as inefficient though 🤔
https://try.kotlinlang.org/#/UserProjects/eeg4jaic5jrlb4utcb6269lsfh/kifrboelan2267bgrijt0lg6o5karelpeeters
04/18/2019, 9:32 PMSmallville7123
04/18/2019, 10:05 PMpablisco
04/18/2019, 10:21 PMSingle Shot
because it’s run on Travis.
I say slightly better because the margin or error on it is wild. Not sure why 🤔karelpeeters
04/18/2019, 11:07 PMpablisco
04/18/2019, 11:08 PMlouiscad
04/19/2019, 6:00 AMkarelpeeters
04/19/2019, 8:07 AMpablisco
04/19/2019, 8:12 AMfor(in)
which also creates an iterator. I'll add a case for an old fashion for loop. Although, at 10k iterations it shouldn't make much difference.karelpeeters
04/19/2019, 8:15 AMpablisco
04/19/2019, 8:17 AMgildor
04/19/2019, 3:49 PMlouiscad
04/19/2019, 5:47 PMpablisco
04/19/2019, 9:27 PMforEach
is not the iterator allocation but rather that each iteration it's making a function call to the closure lambda where as in a traditional for loop it doesn't. Again it can be an early optimization since it only has an effect if we have thousands of elements.
It's something to be mindful about, but always better to optimise if we observe adverse effects. I always like to test apps on old and low power devices to make sure 😁gildor
04/20/2019, 12:27 AMKnown to cause a significant GC pressureSounds like a dogma than a real case. Again, I agree that it make sense to use loop with onDraw, but it's still very specific case and we usually care about it just because we know that you should be careful on such hot methods, but replacing iterator around your code base is exactly premature optimization
each iteration it's making a function call to the closure lambda where as in a traditional for loop it doesn't.It's incorrect for standard Kotlin forEach implementation for collection. It is inlined and after compilation you have just loop using iterator
louiscad
04/20/2019, 7:10 AMpablisco
04/20/2019, 9:06 AMgildor
04/20/2019, 9:09 AMpablisco
04/20/2019, 9:15 AMnoinline
. However, I'm not sure why I'm getting such unreliable results on this one 🤔
https://pablisco.com/kotlin-benchmarks/gildor
04/21/2019, 12:32 AMforEachIndexed
is Kotlin stdlib function that create one more additional object with index and value, Louis above talked about own custom loop, implemented using iteration by index instead of iterator, not about this stdlib function
Something like this:
inline fun <T> List<T>.forEachByIndex(block: (T) -> Unit) {
for (i in 0 until size) {
block(this[i])
}
}
louiscad
04/21/2019, 8:09 AMArray
doesn't allocate an iterator because an Array can't change its size and doesn't count modifications.Smallville7123
04/21/2019, 9:36 AMpablisco
04/21/2019, 12:19 PMfor(in)
is exactly the same as forEach
. In practice, like many things in our field, it depends 😅gildor
04/21/2019, 3:23 PM