MathGroup Archive 2012

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

Search the Archive

Re: Find Position of many elements in a large list.

  • To: mathgroup at smc.vnet.net
  • Subject: [mg127711] Re: Find Position of many elements in a large list.
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Thu, 16 Aug 2012 01:57:17 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-newout@smc.vnet.net
  • Delivered-to: mathgroup-newsend@smc.vnet.net

On 8/15/12 at 3:31 AM, petsie at dordos.net (Peter Pein) wrote:

>Am 14.08.2012 11:05, schrieb benp84 at gmail.com:
>>I have a sorted, 1-dimensional list X of 1,000,000 integers, and a
>>sorted, 1-dimensional list Y of 10,000
>integers.  Most, but not all, of the elements of Y are also elements
>of X.  I'd like to know the positions of the elements in X that are
>also in Y.  What's the fastest way to compute this?

>the fastest way I was able to find is about four times faster than the
>naive approach

>Flatten[Position[x, Alternatives @@ Intersection[x, y], 1]]

>Dropping successively the irrelevant parts of the huge list x,
>searching becomes faster:

>Reap[
>Fold[ Drop[#1, Sow[LengthWhile[#1, Function[x0, x0 <= #2]]]] &, x,
>Intersection[x, y]
>]
>][[2, 1]] // Accumulate

On my system

In[1]:= x = Range[10^6];

In[2]:= y = RandomInteger[10^6, 5]

Out[2]= {94085,367746,149688,500015,542925}

In[3]:= Timing[
  Flatten[Position[x, Alternatives @@ Intersection[x, y], 1]]]

Out[3]= {0.277049,{94085,149688,367746,500015,542925}}

In[4]:= Timing[
  Reap[Fold[Drop[#1, Sow[LengthWhile[#1, Function[x0, x0 <=
#2]]]] &,
      x, Intersection[x, y]]][[2, 1]] // Accumulate]

Out[4]= {0.900927,{94085,149688,367746,500015,542925}}

In[5]:= Timing[
  Ordering[Ordering[Join[y, x]]][[;; Length@y]] -
   Range[0, Length@y - 1]]


Out[5]= {0.021082,{94085,367747,149687,500015,542925}}




  • Prev by Date: Re: V8 slow like a snail
  • Next by Date: Re: Remote kernel, SSH and macintosh
  • Previous by thread: Re: Find Position of many elements in a large list.
  • Next by thread: Re: Find Position of many elements in a large list.