Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

im using Map on a List like this:

cube= {{1, 1, 1}, {1, 1, 2}, {1, 1, 3}, {1, 1, 4}, ... , {5, 5, 4}, {5, 5, 5}}

Mapping the whole List with 125 entries takes like 2.5s.

 Map[Apply[d[[#1, #2, #3]] &, #] &, cube];

{2.552146, Null}

Mapping in two sublists with less than 100 entries the whole thing takes nearly no time.

       Map[Apply[d[[#1, #2, #3]] &, #] &, cube[[1 ;; 99]]],
       Map[Apply[d[[#1, #2, #3]] &, #] &, cube[[100 ;; 125]]]


{0., Null}

Why is there a huge performance gap? An how do I avoid it except splitting my list?

share|improve this question
Btw your cube is equal to Tuples[Range[5],3], just so you know :). – Teake Nutma Jul 16 '14 at 10:56
@TeakeNutma I bet ... here really means {2,9,12},{tweedledee,tweedledum, "tweedleDUN"}. This reminds me of a book by a mathematician I once read which pointed out a remarkable device that physicists have come up with, namely, $\ldots$, which allows them to solve practically any problem simply by omission. – acl Jul 16 '14 at 11:04
Can you show what d is? and isn't this the same : Map[d[[Sequence @@ #]] &, cube]? I cant reproduce this slowness even on much larger lists. – george2079 Jul 16 '14 at 15:13
@george2079 I've added an example in my answer, take a look – acl Jul 16 '14 at 20:55
up vote 9 down vote accepted

If you look at SystemOptions[], like so,

 OpenerView /@
  (Replace[SystemOptions[], Rule[x_, y_] -> List[x, y],

you see that under CompileOptions, if you click on the triangle to open it,

enter image description here

there is an option "MapCompileLength" -> 100. Set it to eg 10 and see it it helps (do SetSystemOptions["CompileOptions" -> {"MapCompileLength" -> 10}]).

This option determines the length of the list above which Mathematica (tries to) compile the function to be mapped.

EDIT: Example:

Here's some data:

Length[cube = Tuples[Range[10], 4]]

And here's a function which is a) inefficient on purpose, b) designed to be compilable as-is (that's why I localise s, so that Compile will work).

d = (Module[{s = 0}, Do[s = s + #[[i]]^2, {i, Length@#}];s] &)

Now, set the auto-compilation length for Map to 100 (the default):

SetSystemOptions["CompileOptions" -> {"MapCompileLength" -> 100}]

and now test:

Quiet@BenchmarkPlot[d /@ # &, cube[[1 ;; #]] &, Range[90, 110]]

enter image description here

share|improve this answer
Maybe I'm dense today. Your example got faster at length 100, which I understand, but the OP's got slower at length 100, which I do not understand. Am I missing something? – Michael E2 Jul 16 '14 at 23:32
@MichaelE2 oops, no, I am dense! You're right. Well then the example is inapplicable. Maybe they should just set the length to infinity and see what happens. – acl Jul 16 '14 at 23:34
@acl maybe the compilation itself is fairly expensive, and not worth the 'payoff'? – Taliesin Beynon Jul 17 '14 at 13:49
@TaliesinBeynon that's the only explanation I could think of, too. Let's see when/if we are shown the function. It might involve lots of subexpressions so that time is spent on optimizing them, for example. – acl Jul 17 '14 at 14:19

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.