The following takes 0.07 seconds on my laptop.

list1 = RandomReal[1.,1000000];
(Outer[Times, list1, RandomReal[1., 2]];) // AbsoluteTiming

But this takes 1.2 seconds:

list1 = RandomReal[1.,1000000];
(Outer[Times, list1, {1.,2.}];) // AbsoluteTiming

What is wrong here?

Both version 10.3 and 7 have this timing result.


1 Answer 1


It seems like RandomReal[1., 2] is automatically a packed array, whereas {1., 2.} is not. Notice;

(Outer[Times, list1, Developer`ToPackedArray@{1., 2.}];) // AbsoluteTiming // First
(* 0.032425 *)


(Outer[Times, list1, {1., 2.}];) // AbsoluteTiming // First
(* 0.542086 *)


list1 = Developer`FromPackedArray@RandomReal[1., 1000000];
(Outer[Times, list1, RandomReal[1., 2]];) // AbsoluteTiming
(* 0.473775 *)

Packed arrays are good for both memory usage and for built-in Mathematica operations that are optimized for packed arrays (for instance). See What is a packed array?.

  • $\begingroup$ Oh, my god! So we users have to pay attention to this? It is ridiculous that using {1.,2.} should be careful :( $\endgroup$
    – matheorem
    Nov 28, 2015 at 15:39
  • $\begingroup$ I am so disappointed with mathematica on this case $\endgroup$
    – matheorem
    Nov 28, 2015 at 15:41
  • $\begingroup$ I think it's a consequence of Mathematica's flexibility. Lists can store any combination of data types, but packed arrays require that Mathematica knows that the data types are all the same and knows what that data type is. Therefore, you have to tell it that the list can be a packed array. Just like any programming language, you have to have a lot of experience and knowledge about the language in order to optimize your code, especially if you are running operations that are numerically intensive. IMO, Mathematica makes basic optimization pretty easy. $\endgroup$
    – march
    Nov 28, 2015 at 15:43
  • 1
    $\begingroup$ I think it's because Plus is Listable and if that attribute is applied first, the array would have to be unpacked; instead, there is a special case when one of the arguments is a packed array. See Rob Knapp's talk (my comment under the what is a packed array question), or maybe it's discussed in one of the answers. Perhaps other functions could be made more efficient. $\endgroup$
    – Michael E2
    Nov 28, 2015 at 16:59
  • 1
    $\begingroup$ @matheorem Outer is compilable. From your comments it sounds like you would prefer the behavior of, for example, outerTimes = Compile[{{l1, _Real, 1}, {l2, _Real, 1}}, Outer[Times, l1, l2]]. $\endgroup$
    – Karsten7
    Nov 28, 2015 at 22:05

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