       Re: Re:Mapping down two lists

• To: mathgroup at smc.vnet.net
• Subject: [mg25553] Re: [mg25515] Re:Mapping down two lists
• From: "Benjamin A. Jacobson" <bjacobson at illumitech.com>
• Date: Sat, 7 Oct 2000 03:35:58 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```Using Mathematica's dot product instead of Plus@@etc. is faster yet.  It's
about 4x faster in the uncompiled version, about 2.5x faster in the
compiled version.  Pure functions and Block constructs are about the
same.(By the way, I think some parentheses were left out in your
expressions--they're added back in here.)

lst1 = Table[{Random[], Random[]}, {1000000}];
lst2 = Table[Random[], {1000000}];
compiledfunction =
Compile[{{x, _Real, 2}, {y, _Real, 1}},
Plus @@ ((First[Transpose[x]] - y)^2)];
compiledfunction2 =
Compile[{{x, _Real, 2}, {y, _Real, 1}}, #.# &[First[Transpose[x]] - y]];

In:=
Plus @@ ((Transpose[lst1][] - lst2)^2) // Timing
compiledfunction[lst1, lst2] // Timing
Block[{v = Transpose[lst1][] - lst2}, v.v] // Timing
#.# &[Transpose[lst1][] - lst2] // Timing
compiledfunction2[lst1, lst2] // Timing

Out={4.517 Second, 166672.}
Out={1.983 Second, 166672.}
Out={1.151 Second, 166672.}
Out={1.092 Second, 166672.}
Out={0.751 Second, 166672.}

Ben Jacobson
Illumitech Inc.
http://www.illumitech.com

At 11:50 PM 10/5/00 -0400, you wrote:
>Many thanks to all those who sent in suggestions as to how to speed up
>calculations involving lists as per my original request below.
>
>I've collected together the suggestions, added some of my own and done some
>timing tests:
>
>In=lst1 = Table[{Random[], Random[]}, {1000000}];
>In=lst2 = Table[Random[], {1000000}];
>
>In=compiledfunction =  Compile[{{x, _Real, 2}, {y,  _Real, 1}},  Plus @@
>(First[Transpose[x]] - y)^2];
>
>In= Plus  @@ ((#[] - #[])^2  &  /@ Transpose[Join[Transpose[lst1],
>{lst2}]]) // Timing
>In= Plus @@ (First[Transpose[lst1]] - lst2)^2 // Timing
>In= Plus @@ (Transpose[lst1][] - lst2)^2 // Timing
>In= Plus @@ (Take[Transpose[lst1], 1][] - lst2)^2 //Timing
>In= Plus @@ (#^2 & /@ (Transpose[lst1][)] - lst2) // Timing
>In= Plus @@ (#^2 & /@ (#[] & /@ lst1 - lst2)) // Timing
>In= compiledfunction[lst1, lst2] // Timing
>
>Out= {10.71 Second, 166795.}
>Out= {4.4 Second, 166795.}
>Out= {4.34 Second, 166795.}
>Out= {4.39 Second, 166795.}
>Out= {4.89 Second, 166795.}
>Out= {7.74 Second, 166795.}
>Out= {2.37 Second, 166795.}
>
> As you can see there is quite a range in the calculation speed. Compiling
>the functions gives a speed enhancement by almost a factor of 2 compared to
>the other fastest time. I noted that, if compiled then, all the functions
>take about the same time to complete hence I only show one of them here. In
>the noncompiled versions its likely that the speed increase arises from
>carrying out a minimum number of calculations. The first in the list clearly
>does the most and takes the longest to complete.
>
>John Satherley

```

• Prev by Date: RE: RE: Hold, HoldForm, ReleaseHold when Plotting multiple functions
• Next by Date: MultiLinear and Linear function
• Previous by thread: Re: Re:Mapping down two lists
• Next by thread: RE: Mapping down two lists