Re: Replace
- To: mathgroup at smc.vnet.net
- Subject: [mg121117] Re: Replace
- From: Armand Tamzarian <mike.honeychurch at gmail.com>
- Date: Mon, 29 Aug 2011 03:47:51 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <j35am1$og8$1@smc.vnet.net> <j37p96$4lk$1@smc.vnet.net>
On Aug 28, 6:09 pm, Peter Pein <pet... at dordos.net> wrote: > Am 27.08.2011 14:22, schrieb Armand Tamzarian: > ... > > > Doh! you wanted "|" not "/" > > > It still looks like using Part will give the fastest solution (on my > > system at least, 8.0.1 OS X 10.6.8) > > > tmp = RandomInteger[{1, 20}, {20000, 2}]; > > > Timing[tmp /. {x_, y_} :> x | y;] > > > Timing[Alternatives @@@ tmp;] > > > Timing[Thread[tmp[[All, 1]] | tmp[[All, 2]]];] > > > Mike > > Transpose seems to be even a bit faster than Part: > > In[1]:= tmp = RandomInteger[{1, 20}, {2000000, 2}]; > {t, r} = Transpose[{ > Timing[tmp /. {x_, y_} :> x | y], > Timing[Apply[Alternatives, tmp, {1}]], > Timing[Thread[tmp[[All,1]] | tmp[[All,2]]]], > Timing[Thread[Alternatives @@ Transpose[tmp]]]} > ]; > t > SameQ @@ r > > Out[3]= {1.56,0.764,0.624,0.531} > Out[4]= True After I posted my solution I realized that the timing would most likely differ if the list contained mixed types, i.e. not packed. Apply becomes the fastest method in that case (incl. Peter's solution): tmp = RandomInteger[{1, 20}, {200000, 2}]; tmp[[1, 1]] = 1.0; {t, r} = Transpose[{Timing[tmp /. {x_, y_} :> x | y], Timing[Apply[Alternatives, tmp, {1}]], Timing[Thread[tmp[[All, 1]] | tmp[[All, 2]]]], Timing[Thread[Alternatives @@ Transpose[tmp]]]}]; t SameQ @@ r Mike