Re: Why Return[] does not work?

• To: mathgroup at smc.vnet.net
• Subject: [mg109486] Re: Why Return[] does not work?
• From: "M.Roellig" <markus.roellig at googlemail.com>
• Date: Thu, 29 Apr 2010 02:54:00 -0400 (EDT)
• References: <hr6ihp\$2f8\$1@smc.vnet.net> <hr8isl\$55h\$1@smc.vnet.net>

```On 28 Apr., 13:00, Dims <dim... at hotmail.com> wrote:
> On 28 =D0=B0=D0=BF=D1=80, 09:59, "M.Roellig" <markus.roel... at googlemail.com>
> > wrote:
> > In[2]:= Intersection[{a, b}, {c, a}] =!= {}
>
> But would Mathematica optimize evaluation of this expression, i.e.,
> terminate searching for common elements if one already found?

No,

it returns all common elements. But it is very fast. The Do approach
scales terribly with the length of the list (N^2) and the position of
the first intersecting element. Here is an example timing for two lists of
length 10000

RandomSeed[123456789];
AbsoluteTiming@Table[Module[{listA, listB},
listA =
Insert[RandomReal[{1, 1000}, 10000], 3333,
RandomInteger[{1, 10000}]];
listB =
Insert[RandomReal[{1001, 2000}, 10000], 3333,
RandomInteger[{1, 10000}]];
intersects[listA, listB]], {10}]

{537.7829457, {True, True, True, True, True, True, True, True, True,
True}}

The same with the built in command is almost instantaneous

RandomSeed[123456789];
AbsoluteTiming@Table[Module[{listA, listB},
listA =
Insert[RandomReal[{1, 1000}, 10000], 3333,
RandomInteger[{1, 10000}]];
listB =
Insert[RandomReal[{1001, 2000}, 10000], 3333,
RandomInteger[{1, 10000}]];
Intersection[listA, listB] =!= {}], {10}]

{0.2028004, {True, True, True, True, True, True, True, True, True,
True}}

Markus

```

• Prev by Date: Re: Why Return[] does not work?
• Next by Date: Re: How to disable Integrate[] ?
• Previous by thread: Re: Why Return[] does not work?
• Next by thread: exporting graphics as PDF