MathGroup Archive 2008

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

Search the Archive

Re: Fast way to select those elements from a list that are in another

  • To: mathgroup at smc.vnet.net
  • Subject: [mg87071] Re: Fast way to select those elements from a list that are in another
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Mon, 31 Mar 2008 02:02:43 -0500 (EST)
  • Organization: The Open University, Milton Keynes, UK
  • References: <frt574$sj8$1@smc.vnet.net>

Andrew Moylan wrote:

> Two lists of integers:
> 
> {a, b} = Table[RandomInteger[10000, 1000], {2}];
> 
> Which elements from a are in b?
> 
> Select[a, MemberQ[b, #] &]; // Timing
>>> {0.351, Null}
> 
> It takes about 0.351 seconds (on my slow laptop). Specialised
> functions for related tasks, such as Intersection[a, b], are much
> faster:
> 
> Intersection[a, b]; // Timing
>>> {0., Null}
> 
> Using Intersection, here's a somewhat faster way to select those
> elements from a that are in b:
> 
> With[
>    {c = Intersection[a, b]},
>    Select[a, MemberQ[c, #] &]
>    ]; // Timing
>>> {0.09, Null}
> 
> Is there a better, faster way to do this?

Andrew,

I have came up with the following, which is, I believe, the fastest 
among all solutions that have been posted (as of today):

     Pick[a, Thread[a == b]]

For instance,

{a, b} = Table[RandomInteger[10000, 1000], {2}];

s1 = Select[a, MemberQ[b, #] &]; // Timing // First

s2 = With[{c = Intersection[a, b]}, Select[a, MemberQ[c, #] &]]; //
   Timing // First

s3 = Pick[a, Thread[a == b]]; // Timing // First

s1 === s2 === s3

(*

0.127442

0.01327

0.000111

True

*)

Regards,
-- 
Jean-Marc


  • Prev by Date: Re: IsIntegerOrFloat
  • Next by Date: Any body can help me solve the equation with piecewise function
  • Previous by thread: Re: Fast way to select those elements from a list that are in another
  • Next by thread: RE: Fast way to select those elements from a list that are in another