Re: Cases, Throw, and Catch
- To: mathgroup at smc.vnet.net
- Subject: [mg24203] Re: [mg24182] Cases, Throw, and Catch
- From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
- Date: Fri, 30 Jun 2000 01:57:30 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
This is indeed fast. However compare it with:
xtab = Table[x, {10^6}];
In[2]:=
Catch @ Dog[xtab, Throw[{x}], Infinity, 1] // Timing
Out[2]=
{0. Second, {x}}
As you can see the not very well known function Dog is pretty fast too:)
Seriously, the point is that Cases plays no role in your Catch and Throw
example. All that happens is that when the arguments of Cases (or Dog) are
evaluated x is Thrown and caught.
Another demonstration:
In[3]:=
Cases[xtab, y, Infinity, 1] // Timing
Out[3]=
{1.38333 Second, {}}
In[4]:=
Catch @ Cases[xtab, Throw[{y}], Infinity, 1] // Timing
Out[4]=
{0. Second, {y}}
--
Andrzej Kozlowski
Toyama International University, JAPAN
For Mathematica related links and resources try:
<http://www.sstreams.com/Mathematica/>
on 6/29/00 11:51 AM, Mark Fisher at me.fisher at atl.frb.org wrote:
> I just noticed that it is much faster to Throw and Catch from Cases than
> to have Cases return the first match on its own. Does anyone know why
> this is so and what, if anything, is to be learned?
>
> Here is the example:
>
> In[1]:=
> xtab = Table[x, {10^6}];
>
> In[2]:=
> Cases[xtab, x, Infinity, 1] // Timing
>
> Out[2]=
> {0.172 Second, {x}}
>
> In[3]:=
> Catch @ Cases[xtab, Throw[{x}], Infinity, 1] // Timing
>
> Out[3]=
> {0. Second, {x}}
>
> In[4]:=
> ytab = Append[xtab, y];
>
> In[5]:=
> Cases[ytab, y, Infinity, 1] // Timing
>
> Out[5]=
> {0.437 Second, {y}}
>
> In[6]:=
> Catch @ Cases[ytab, Throw[{y}], Infinity, 1] // Timing
>
> Out[6]=
> {0. Second, {y}}
>
> --Mark.
>
> Mark Fisher
> Economic Advisor
> Research Department
> Federal Reserve Bank of Atlanta
> Atlanta, GA 30303
> 404-521-8757 (voice)
> 404-521-8810 (fax)
> mark.fisher at atl.frb.org
>
> personal web page: www.markfisher.net
>