       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=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];
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= 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
In=#.# &@(lst1[[All, 1]] - lst2) // Timing
In=With[{ll = lst1[[All, 1]] - lst2}, ll.ll] // Timing
In=compiledfunction2[lst1, lst2] // Timing
In=compiledfunction3[lst1, lst2] // Timing

Out= {10.06 Second, 166597.}
Out= {4.12 Second, 166597.}
Out= {4.11 Second, 166597.}
Out= {4.23 Second, 166597.}
Out= {4.62 Second, 166597.}
Out= {7.36 Second, 166597.}
Out= {2.25 Second, 166597.}
Out={1.21 Second, 166597.}
Out={1.21 Second, 166597.}
Out={0.82 Second, 166597.}
Out={0.82 Second, 166597.}

John Satherley

```

• Prev by Date: Re: tracing lines on surface-graphics
• Next by Date: Re: tracing lines on surface-graphics
• Previous by thread: Re: Re:Mapping down two lists
• Next by thread: ISSAC 2001 - Call for Papers