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.
Answer
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 *)
whereas
(Outer[Times, list1, {1., 2.}];) // AbsoluteTiming // First
(* 0.542086 *)
Also:
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?.
Comments
Post a Comment