MathGroup Archive 2005

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

Search the Archive

Re: Complement replacement

  • To: mathgroup at smc.vnet.net
  • Subject: [mg58617] Re: [mg58608] Complement replacement
  • From: Andrzej Kozlowski <akozlowski at gmail.com>
  • Date: Sun, 10 Jul 2005 16:51:35 -0400 (EDT)
  • References: <200507100912.FAA06500@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On 10 Jul 2005, at 18:12, konstantpi at mail15.com wrote:

> hi
> in the list:
> pp=Table[Random[Integer, {1, 1000}], {i, 1000}];
> how could i know which numbers from 1 to 1000 does not exist in the
> pp List.
> but without using:
> Complement[Table[i,{i,1000}],pp]
> regards
>
>


Perhaps you should explain why you do not want to use Complement as  
it will be very hard if not impossible to match its performance.  
Indeed, on ny machine I get:

In[1]:=
pp=Table[Random[Integer, {1, 1000}], {i, 1000}];

In[2]:=
a=Complement[Range[1000],pp];//Timing

Out[2]=
{0.001657 Second,Null}

while using the most obvious alternative:

In[3]:=
b=Select[Range[1000],Not[MemberQ[pp,#]]&];//Timing

Out[3]=
{0.408538 Second,Null}

In[4]:=
Union[b]==a

Out[4]=
True

The difference in performance is huge. There are a number of ways  
that give a better performance but I can't find any that beets  
Complement. Here s one that is at least better than the most obvious  
way above. First define a function f:

SetAttributes[f, Listable]

Scan[(f[#]=Sequence[])&,pp];//Timing

{0.020849 Second,Null}

f[x_] := x

(note that evaluating this defintion itself takes more time than  
running Complement)

now:

In[8]:=
c=f[Range[1000]];//Timing

Out[8]=
{0.008274 Second,Null}

In[9]:=
Union[c]==a

Out[9]=
True

Andrzej Kozlowski

Chiba, Japan


  • Prev by Date: Re: Complement replacement
  • Next by Date: Re: SeedRandom and $SessionID
  • Previous by thread: Re: Complement replacement
  • Next by thread: Re: Complement replacement