Re: Performance of Array Addition
- To: mathgroup at smc.vnet.net
- Subject: [mg90978] Re: [mg90952] Performance of Array Addition
- From: Oliver Ruebenkoenig <ruebenko at wolfram.com>
- Date: Fri, 1 Aug 2008 03:01:32 -0400 (EDT)
- References: <200807310657.CAA07753@smc.vnet.net>
Hi Jeremy, On Thu, 31 Jul 2008, Jeremy 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 > How about, AbsoluteTiming[ res3 = Plus @@@ Transpose[array, {1, 3, 2}]; ] Hth, Oliver