does an index-based `for` loop over a `List`, have...
# getting-started
y
does an index-based
for
loop over a
List
, have equivalent performance to using
subList()
?
p
probably depends on your list implementation, the size of the list, the number of indices you’re traversing, etc
e
for most lists, subList is just a view that offsets and limits the indices. there may be some indirection overhead. probably not much, measure yourself if you want to know
and for most (but not all) lists
for (x in list)
is more expensive than
for (i in list.indices) val x = list[i]
as the latter avoids going through an iterator. but most code should not bother with such microoptimizations
y
that sounds good enough to me, to be honest. "no copy under-the-hood" is already a strong guarantee. also, I forgot iterators in Kotlin are not quite zero cost
as in, relatively farther away from zero cost