Re: Better Way of Testing and Replacing List Elements?
- To: mathgroup at smc.vnet.net
- Subject: [mg104045] Re: Better Way of Testing and Replacing List Elements?
- From: "E. Martin-Serrano" <eMartinSerrano at telefonica.net>
- Date: Sat, 17 Oct 2009 07:01:07 -0400 (EDT)
- References: <200910161121.HAA01370@smc.vnet.net> <4AD881A4.5070001@wolfram.com>
Carl, Thank you (and sorry). Good to know. I assumed that *ToPackedArray* was used automatically by both *RandomChoice* and *Select* as they deal with large lists in this case. Emilio. -----Original Message----- From: Carl Woll [mailto:carlw at wolfram.com] Sent: Friday, October 16, 2009 3:22 PM To: E. Martin-Serrano Cc: mathgroup at smc.vnet.net Subject: [mg104045] Re: [mg104039] Re: Better Way of Testing and Replacing List Elements? E. Martin-Serrano wrote: > Hi, > > After several tries the faster seems to be xxx*UnitStep[xxx] > > See: > > In[1]:= xxx = RandomChoice[{-1, 1}, 1000000]; > > In[2]:= Length@Select[xxx, (# == -1) &] > > Out[2]= 500002 > > In[3]:= Timing[ ReplacePart[xxx, Position[Map[Negative, xxx], True] -> 0];] > > Out[3]= {2.672, Null} > > In[4]:= Timing[(xxx /. _?Negative -> 0);] > > Out[4]= {0.796, Null} > > In[5]:= Timing[(xxx*UnitStep[xxx]);] > > Out[5]= {0.063, Null} > > In[6]:= Timing[Clip[xxx, {0, Infinity}];] > > Out[6]= {0.171, Null} > > In[7]:= Timing[(Max[#, 0] & /@ xxx);] > > Out[7]= {1.406, Null} > > In[8]:= Timing[(xxx /. (x_ /; x < 0) -> 0);] > > Out[8]= {0.859, Null} > > In[9]:= Timing[(xxx Unitize[Sign[xxx] + 1]);] > > Out[9]= {0.078, Null} > > > Emilio > As long as the list is packed, the fastest will be Clip. In your example, xxx is not packed: In[201]:= xxx = RandomChoice[{-1, 1}, 1000000]; In[202]:= Developer`PackedArrayQ[xxx] Out[202]= False Let's pack it up, and compare again: paxxx = Developer`ToPackedArray[xxx]; In[204]:= Clip[paxxx, {0, Infinity}]; // Timing Out[204]= {0., Null} In[205]:= paxxx UnitStep[paxxx]; // Timing Out[205]= {0.032, Null} Carl Woll Wolfram Research > > -----Original Message----- > From: Bill Rowe [mailto:readnews at sbcglobal.net] > Sent: Thursday, October 15, 2009 12:18 PM > To: mathgroup at smc.vnet.net > Subject: [mg104039] [mg104006] Re: Better Way of Testing and Replacing List Elements? > > On 10/13/09 at 11:19 PM, careysub at gmail.com (careysub) wrote: > > >> The code below replaces the negative values in a list with zero, and >> is an example of a type of operation I use a lot: >> > > >> xxx = {1, 2, 3, 4, 5, -6, -7, 8, 9, 10, -1, 11, 12}; >> ReplacePart[xxx, Position[Map[Negative, xxx], True] -> 0] >> > > >> Is there a "better" way of doing this (fewer function calls, more >> efficient)? >> > > >> My feeling is that I'm doing this in an awkward way. >> > > Other ways would be: > > In[1]:= xxx = {1, 2, 3, 4, 5, -6, -7, 8, 9, 10, -1, 11, 12}; > > In[2]:= Clip[xxx, {0, Infinity}] > > Out[2]= {1,2,3,4,5,0,0,8,9,10,0,11,12} > > In[3]:= xxx /. a_?Negative :> 0 > > Out[3]= {1,2,3,4,5,0,0,8,9,10,0,11,12} > > In[4]:= xxx Unitize[Sign[xxx] + 1] > > Out[4]= {1,2,3,4,5,0,0,8,9,10,0,11,12} > > =46or small data sets, the timing of these won't be much > different. For large data sets, Clip is probably the fastest. > > > >
- References:
- Re: Better Way of Testing and Replacing List Elements?
- From: "E. Martin-Serrano" <eMartinSerrano@telefonica.net>
- Re: Better Way of Testing and Replacing List Elements?