is an operation that loops through a collection on...
# announcements
h
is an operation that loops through a collection once performing two operations more/less/equal performance to an operation that loops through the collection twice, performing the first operation then the second?
Copy code
myList.toArray { it + 1 }
v
Copy code
myList.map { it + 1 }.toArray()
to be precise, I'm wondering if Kotlin performs equally well, not simply that they have the same runtime complexity.
k
Doesn't
map
return a list?
d
In terms of time complexity (O) notation, yes it is. The prior might be quicker though when you factor in cache misses, but this is pure speculation.
h
I thought it returned
Iterable<int>
in this case, but I might be wrong (been writing too much c#). Not really the point, but touche.
d
It is equal* is what I mean by yes.
I didn't look at your examples.
k
I'd assume that
toArray
doesn't use an intermediate so it's faster. However once the JIT desides to optimise it, it becomes hard to see if there is any difference. If you use graalVM I think it's the same, but the standard JVM is most likely a tad slower for the
map{}.toArray
case
h
either way, i guess if this distinction here matters for your project, Kotlin isn't the right language to go with either way. probably should be using c
d
All I see here is a lot of assumptions. Assumptions should not be used for important decisions like that.
e
the second one will make list internally by map?