6
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?

István Zachar
  • 47,032
  • 20
  • 143
  • 291
expression
  • 5,642
  • 1
  • 19
  • 46

1 Answers1

7

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.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371