Re: Mapping tag-value lists to a third

• To: mathgroup at smc.vnet.net
• Subject: [mg132470] Re: Mapping tag-value lists to a third
• From: Ray Koopman <koopman at sfu.ca>
• Date: Mon, 24 Mar 2014 00:20:11 -0400 (EDT)
• Delivered-to: l-mathgroup@mail-archive0.wolfram.com
• Delivered-to: l-mathgroup@wolfram.com
• Delivered-to: mathgroup-outx@smc.vnet.net
• Delivered-to: mathgroup-newsendx@smc.vnet.net

On Sun, Mar 23, 2014 @ 02:00 AM, Bill Rowe <readnews at sbcglobal.net> wrote:

> On 3/22/14 at 12:06 AM, koopman at sfu.ca (Ray Koopman) wrote:
>
>> For v5+ users,
>>
>> val[[ SparseArray[tag] /. SparseArray[_,_,_,d_] :> Flatten@d[[2,2]]
>> ]]
>>
>> may be faster than Pick[val,tag,1].
>
> For what it is worth
>
> In[1]:= n = 10^7;
> val = RandomReal[1, n];
> tag = RandomInteger[1, n];
>
> In[4]:= Timing[c = Pick[val, tag, 1];]
>
> Out[4]= {0.137875,Null}
>
> In[5]:= Timing[
>  b = val[[SparseArray[tag] /.
>       SparseArray[_, _, _, d_] :> Flatten@d[[2, 2]]]];]
>
> Out[5]= {0.255513,Null}
>
> In[6]:= b == c
>
> Out[6]= True
>
> In[7]:= \$Version
>
> Out[7]= 9.0 for Mac OS X x86 (64-bit) (January 24, 2013)

Timing appears to be release-dependent.

In[1]:= n = 10^6;
val = Table[Random[Real],{n}];
tag = Table[Random[Integer],{n}];

In[4]:= Timing[a = Pick[val,tag,1];]

Out[4]= {0.65 Second,Null}

In[5]:= Timing[b = val[[ SparseArray[tag] /.
SparseArray[_,_,_,d_] :> Flatten@d[[2,2]] ]];]

Out[5]= {0.1 Second,Null}

In[6]:= a == b

Out[6]= True

In[7]:= \$Version

Out[7]= 5.2 for Mac OS X (June 20, 2005)

• Prev by Date: Re: Do we need a When function?
• Next by Date: random walk
• Previous by thread: Re: Mapping tag-value lists to a third
• Next by thread: EventHandler in Graphics