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.

  • 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. – David Arno Aug 17 '17 at 08:06
  • Stupid question: Would it be a kind of deep copy or just dealing with references? Or am completly missing the point with this question? – Korinna Aug 17 '17 at 08:20
  • 2
    Possible duplicate of [Is micro-optimisation important when coding?](https://softwareengineering.stackexchange.com/questions/99445/is-micro-optimisation-important-when-coding) – gnat Aug 17 '17 at 08: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 – jk. Aug 17 '17 at 08:55
  • 3
    map is *at worst* a for loop behind a function call. – Caleth Aug 17 '17 at 09:10
  • 1
    @jk You should make your comment an actual answer (with some elaboration). It's the best response so far. – user949300 Aug 17 '17 at 15:31
  • 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. – Robert Harvey Aug 17 '17 at 16:34
  • @RobertHarvey apparently you missed what it asks: "Is one any more efficient than the other?" – gnat Aug 17 '17 at 19:04

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,216
  • 1
  • 33
  • 43
  • 1
    `map` is pure (in the sense that it doesn't add impurity), but impure `f` implies impure `map f` – Caleth Aug 18 '17 at 08: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.

Karl Bielefeldt
  • 146,727
  • 38
  • 279
  • 479
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
  • 3,562
  • 1
  • 19
  • 24
  • 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. – FirstLastname Aug 18 '17 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 – Alexander Aug 18 '17 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.

Kevin
  • 731
  • 1
  • 4
  • 15