Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.
Outer[Plus, ##] & @@ Array[{1, 2, 6, 24, 120, 720, 5040, 40320, 362880} &, 6]; // AbsoluteTiming
(* {0.979056, Null} *)

Outer[Plus, ##] & @@ Array[Range[9]! &, 6]; // AbsoluteTiming
(* {0.952055, Null} *)

Outer[Plus, ##] & @@ Array[Gamma[Range@9 + 1] &, 6]; // AbsoluteTiming
(* {0.010001, Null} *)

See the elapsed time, I used v9 on Win7 32bit. On v8, both of them are slow. How to explain this? Is it possible to make it faster on v8?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

I actually get the same timing for all three versions (using v7), but this is almost certainly a matter of packing. You can use Developer`ToPackedArray to convert your sub-arrays, as well as Developer`PackedArrayQ to check their status.

In this case on my system:

set = {1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
packed = Developer`ToPackedArray[set];

Outer[Plus, ##] & @@ Table[set, {6}];    // AbsoluteTiming
Outer[Plus, ##] & @@ Table[packed, {6}]; // AbsoluteTiming
{0.2300003, Null}

{0., Null}

Another example:

a = Array[#2! &, {6, 9}];
Developer`PackedArrayQ /@ a
{False, False, False, False, False, False}
b = Developer`ToPackedArray[a];
Developer`PackedArrayQ /@ b
{True, True, True, True, True, True}
Outer[Plus, ##] & @@ a; // AbsoluteTiming
Outer[Plus, ##] & @@ b; // AbsoluteTiming
{0.2400004, Null}

{0., Null}

Note that you will not be able to pack a list or array that contains integers larger than the maximum machine-size integer on your system.

share|improve this answer

Your Answer

 
discard

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.