Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2000
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2000

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

Search the Archive

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[14]:=
Plus @@ ((Transpose[lst1][[1]] - lst2)^2) // Timing
compiledfunction[lst1, lst2] // Timing
Block[{v = Transpose[lst1][[1]] - lst2}, v.v] // Timing
#.# &[Transpose[lst1][[1]] - lst2] // Timing
compiledfunction2[lst1, lst2] // Timing

Out[14]={4.517 Second, 166672.}
Out[15]={1.983 Second, 166672.}
Out[16]={1.151 Second, 166672.}
Out[17]={1.092 Second, 166672.}
Out[18]={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[1]=lst1 = Table[{Random[], Random[]}, {1000000}];
>In[2]=lst2 = Table[Random[], {1000000}];
>
>In[3]=compiledfunction =  Compile[{{x, _Real, 2}, {y,  _Real, 1}},  Plus @@
>(First[Transpose[x]] - y)^2];
>
>In[4]= Plus  @@ ((#[[1]] - #[[3]])^2  &  /@ Transpose[Join[Transpose[lst1],
>{lst2}]]) // Timing
>In[5]= Plus @@ (First[Transpose[lst1]] - lst2)^2 // Timing
>In[6]= Plus @@ (Transpose[lst1][[1]] - lst2)^2 // Timing
>In[7]= Plus @@ (Take[Transpose[lst1], 1][[1]] - lst2)^2 //Timing
>In[8]= Plus @@ (#^2 & /@ (Transpose[lst1][[1])] - lst2) // Timing
>In[9]= Plus @@ (#^2 & /@ (#[[1]] & /@ lst1 - lst2)) // Timing
>In[10]= compiledfunction[lst1, lst2] // Timing
>
>Out[4]= {10.71 Second, 166795.}
>Out[5]= {4.4 Second, 166795.}
>Out[6]= {4.34 Second, 166795.}
>Out[7]= {4.39 Second, 166795.}
>Out[8]= {4.89 Second, 166795.}
>Out[9]= {7.74 Second, 166795.}
>Out[10]= {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