Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Performance of Array Addition

  • To: mathgroup at smc.vnet.net
  • Subject: [mg90982] Re: [mg90952] Performance of Array Addition
  • From: DrMajorBob <drmajorbob at att.net>
  • Date: Fri, 1 Aug 2008 03:02:16 -0400 (EDT)
  • References: <12864392.1217497029287.JavaMail.root@m08>
  • Reply-to: drmajorbob at longhorns.com

Here are timings of your methods and a couple of my own.

First with integers, then with reals.

Clear[integers]
integers[d1_, d2_] := RandomInteger[10, {d1, d2, 3}]
array = integers[3000, 4000];
Timing[one =
    array[[All, All, 1]] + array[[All, All, 2]] +
     array[[All, All, 3]];]
Timing[two = Map[Total, array, {2}];]
Timing[three = Plus @@ Transpose[array, {2, 3, 1}];]
Timing[four = Total@Transpose[array, {2, 3, 1}];]
one == two == three == four

{1.06188, Null}

{12.6128, Null}

{0.623087, Null}

{0.69472, Null}

True

(I test first with integers so that there's less ambiguity in the test for  
equality.)

Clear[reals]
reals[d1_, d2_] := RandomInteger[10, {d1, d2, 3}]
array = reals[3000, 4000];
Timing[one =
    array[[All, All, 1]] + array[[All, All, 2]] +
     array[[All, All, 3]];]
Timing[two = Map[Total, array, {2}];]
Timing[three = Plus @@ Transpose[array, {2, 3, 1}];]
Timing[four = Total@Transpose[array, {2, 3, 1}];]
one == two == three == four

{1.07193, Null}

{12.323, Null}

{0.624647, Null}

{0.705247, Null}

True

> The advantage of Map[Total, array, {2}] is that it allows arrays of
> dimension {d1,d2,d3}, with d3 arbitrary.

I believe my methods also have that property.

integers[d1_, d2_, d3_] := RandomInteger[10, {d1, d2, d3}]
array = integers[3000, 400, 100];
Timing[two = Map[Total, array, {2}];]
Timing[three = Plus @@ Transpose[array, {2, 3, 1}];]
Timing[four = Total@Transpose[array, {2, 3, 1}];]
two == three

{2.13325, Null}

{2.18372, Null}

{1.98925, Null}

True

array = integers[300, 4000, 100];
Timing[two = Map[Total, array, {2}];]
Timing[three = Plus @@ Transpose[array, {2, 3, 1}];]
Timing[four = Total@Transpose[array, {2, 3, 1}];]
two == three

{2.13839, Null}

{2.18131, Null}

{1.97734, Null}

True

> Is there a way to get closer to the speed advantages of hand-coding
> the addition of the final dimension, while
> allowing the number of elements in the final dimension to be
> arbitrary?

On average, I suspect Total@Transpose is competitive.

Bobby

On Thu, 31 Jul 2008 01:57:38 -0500, Jeremy <jeremytfox at mac.com> wrote:

> Let there be, for example, an array of reals with dimensions
> {d1,d2,3}.
>
> I have noticed that the command
>
> array[[All,All,1]] + array[[All,All,2]] + array[[All,All,3]];
>
> is much faster than
>
> Map[Total, array, {2}];
>
> even though they return the same answer.
>
> The advantage of Map[Total, array, {2}] is that it allows arrays of
> dimension {d1,d2,d3}, with d3 arbitrary.
>
> Is there a way to get closer to the speed advantages of hand-coding
> the addition of the final dimension, while
> allowing the number of elements in the final dimension to be
> arbitrary?
>
> Thanks, Jeremy
>
>



-- 
DrMajorBob at longhorns.com


  • Prev by Date: Re: Re: When is a List not a List?
  • Next by Date: Re: Re: NMaximize Questions
  • Previous by thread: Re: Performance of Array Addition
  • Next by thread: Re: Performance of Array Addition