RE: Mapping down two lists
- To: mathgroup at smc.vnet.net
- Subject: [mg25595] RE: Mapping down two lists
- From: "Dr J. Satherley" <js1 at liverpool.ac.uk>
- Date: Wed, 11 Oct 2000 03:50:36 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Hi MathGroup Further to my recent posting to the group two more very quick constructions have been suggested which are both superior to all previous suggestions. 1) Carl Woll suggested #.#&@(lst1[[All,1]]-lst2) which uses the internal Dot product to do the square and sum. This is very nifty and quicker than my fastest compiled function. 2) Daniel Lichtblau suggested compiling With[{ll=lst1[[All,1]]-lst2}, ll.ll] which also makes use of Dot. I've added these to my existing list both in normal and compiled forms to compare the timings. Both of these constructions give identical speeds and are only slightly slower than their compiled versions. 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]; compiledfunction2 = Compile[{{x, _Real, 2}, {y, _Real, 1}}, #.# &@(x[[All, 1]] - y)]; compiledfunction3 = Compile[{{x, _Real, 2}, {y, _Real, 1}}, With[{ll = x[[All, 1]] - y}, ll.ll]]; 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 In[11]=#.# &@(lst1[[All, 1]] - lst2) // Timing In[12]=With[{ll = lst1[[All, 1]] - lst2}, ll.ll] // Timing In[13]=compiledfunction2[lst1, lst2] // Timing In[14]=compiledfunction3[lst1, lst2] // Timing Out[4]= {10.06 Second, 166597.} Out[5]= {4.12 Second, 166597.} Out[6]= {4.11 Second, 166597.} Out[7]= {4.23 Second, 166597.} Out[8]= {4.62 Second, 166597.} Out[9]= {7.36 Second, 166597.} Out[10]= {2.25 Second, 166597.} Out[11]={1.21 Second, 166597.} Out[12]={1.21 Second, 166597.} Out[13]={0.82 Second, 166597.} Out[14]={0.82 Second, 166597.} John Satherley