2

Aesthetically and mathematically a map(f, X) function seems great: in just two arguments it tells what is going to happen: we are going to use function f to transform each element from X to create a new array Y.

However I do not know what is happening under the bonnet of either for or map; and my reasons for liking map could be argued to be superficial. Is one any more efficient than the other?

Note: the question is not about micro-optimisation: the question is a broad (multi-criterion) comparison between two specific methods (that seem to achieve the same objective) and why would one use one or another.

4 Answers4

8

97% of the time you should use the one that is easier to understand and therefore easier to get correct.

I'd also note that there is also an implicit immutable vs mutable data issue here. map is always immutable and will always return a new collection of things, for may or may not be used immutably.

I'd lean toward immutability as easier to get correct moving to a mutable solution only if it was required. I'd therefore lean towards using map as it better expresses this intent.

jk.
  • 10,306
1

map has other advantages, the largest two being that for loops don't have return types and can't be part of a lazily-evaluated pipeline. The first advantage means the compiler will help point out more of your mistakes earlier. The second advantage means you can write code like this (in Scala):

giantArray.view map f find {_ == 3}

Since a view is evaluated lazily, f will get run only on as many elements of giantArray as are needed to find a result of 3. This is decidedly more efficient than converting the entire array, but you can write your code as if you had converted the entire array, which makes it much simpler to write.

Karl Bielefeldt
  • 148,830
1

Optimise for readability above all else. It's not just superficial. It reduces clutter and speeds/improves development.

In the end, you're trying to convey to the reader that a mapping operation is being done.

In the map case, it's immediately obvious that mapping operation is being done.

In the case of a for case, it's not instantly clear. The reader will have to think about the code for a little before identifying it as a mapping. It adds an extra step.

On the topic of performance, in the for loop case, people very often forget to reserve the capacity necessary in their result array. As a result, the repeated additions to the array incur reallocation overhead.

Any decent implementation of map will automatically instantiate an output array at least as big as the input array it was called on. If the resulting array is immutable, the output array can be exactly the same size as the input (without concern for future insertions). It's one less thing to forget.

Alexander
  • 5,185
0

Mapping all of the elements from one array to a new one is an O(n) process one way or another with minimal overhead. In fact, many implementations of a map function will simply use a for loop behind the scenes! So there will rarely be a nonnegligible difference in performance between using a for loop or calling a map function.

Kevin
  • 731