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.

8
  • 5
    Which is more efficient will depend on the language/environment, the way each is written, the data to be transformed etc. The only way for you to know which is more efficient for you is to try both and test the speed of both. Commented Aug 17, 2017 at 8:06
  • 2
    Possible duplicate of Is micro-optimisation important when coding? Commented Aug 17, 2017 at 8:41
  • 3
    97% of the time you should use the one that is easier to understand and :. easier to get correct. I'd also note that there is also an implicit immutable vs mutable data issue here. again I'd lean toward immutability as easier to get correct moving to a mutable solution only if it was required Commented Aug 17, 2017 at 8:55
  • 3
    map is at worst a for loop behind a function call. Commented Aug 17, 2017 at 9:10
  • 2
    @gnat: That question doesn't seem to bear much resemblance to this one, nor do the answers provide much illumination on this specific issue. Commented Aug 17, 2017 at 16:34

4 Answers 4

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.

1
  • 1
    map is pure (in the sense that it doesn't add impurity), but impure f implies impure map f Commented Aug 18, 2017 at 8:27
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.

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.

2
  • To be entirely fair, this can be somewhat language dependent. In the C/Java world for instance, everyone is so used to for loops that using a map instead would arguably incur more overhead for the reader. Commented Aug 18, 2017 at 17:58
  • @FirstLastname True, but I would prefer not to cater to the lowest common denominator. In any case, that's changing, as functional programming is becoming more and more mainstream in Java Commented Aug 18, 2017 at 18:10
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.