Re: Re: style question
- To: mathgroup at smc.vnet.net
- Subject: [mg79565] Re: [mg79529] Re: style question
- From: Carl Woll <carlw at wolfram.com>
- Date: Sun, 29 Jul 2007 00:16:12 -0400 (EDT)
- References: <f84jp8$qmp$1@smc.vnet.net> <200707280941.FAA00166@smc.vnet.net>
Ray Koopman wrote: >On Jul 24, 3:20 am, Yaroslav Bulatov <yarosla... at gmail.com> wrote: > > >>What is the recommended way of counting the number of matches in two >>lists? >> >>The natural way would be to thread over Equal, but Equal will evaluate >>before Thread gets to it. The method below works, but somehow feels >>wrong >> >>m1 = RandomInteger[{1}, {10^5, 2, 2}]; >>m2 = RandomInteger[{1}, {10^5, 2, 2}]; >>matches = Thread[temporary[m1, m2]] /. temporary -> Equal; >>Count[matches, True] >> >> > >When comparing the time take by two methods of counting, I >found a surprising dependence on how the data are generated. >(Parenthesized times are from a replication.) > >Does anyone know why the first method of generation is so much >slower, but its count times so much faster and the difference >between the count times so much bigger? > >Is the extra generation time being spent putting the data >into a format that happens to facilitate the counting? > > The difference is most likely due to unpacking packed arrays. All other things being equal, code that unpacks will typically be much slower than code that doesn't unpack. One way to see whether unpacking is an issue is to set a system option: Version 5.2: Developer`SetSystemOptions["UnpackMessage"->True] Version 6. SetSystemOptions["PackedArrayOptions"->"UnpackMessage"->True] Now, run your code and see where the unpacking occurs. Carl Woll Wolfram Research >In[5]:= Quit >In[1]:= SeedRandom[1]; >Timing[{m1,m2} = Table[Random[Integer],{2},{1*^6},{2},{2}];] >Timing[Count[MapThread[Equal,{m1,m2}],True]] >Timing[Count[Equal @@@ Transpose@{m1,m2}, True]] >Out[2]= {4.11 (4.11) Second, Null} >Out[3]= {1.66 (1.71) Second, 62479} >Out[4]= {1.98 (2.02) Second, 62479} > >In[5]:= Quit >In[1]:= SeedRandom[1]; >Timing[m1m2 = Table[Random[Integer],{2},{1*^6},{2},{2}];] >Timing[Count[MapThread[Equal,m1m2],True]] >Timing[Count[Equal @@@ Transpose@m1m2, True]] >Out[2]= {2.45 (2.44) Second, Null} >Out[3]= {3.66 (3.72) Second, 62479} >Out[4]= {3.56 (3.49) Second, 62479} > >In[5]:= Quit >In[1]:= SeedRandom[1]; >Timing[{m1 = Table[Random[Integer],{1*^6},{2},{2}], > m2 = Table[Random[Integer],{1*^6},{2},{2}]};] >Timing[Count[MapThread[Equal,{m1,m2}],True]] >Timing[Count[Equal @@@ Transpose@{m1,m2}, True]] >Out[2]= {2.51 (2.53) Second, Null} >Out[3]= {3.61 (3.71) Second, 62479} >Out[4]= {3.56 (3.61) Second, 62479} > >In[5]:= Quit >In[1]:= SeedRandom[1]; >Timing[m1 = Table[Random[Integer],{1*^6},{2},{2}]; > m2 = Table[Random[Integer],{1*^6},{2},{2}];] >Timing[Count[MapThread[Equal,{m1,m2}],True]] >Timing[Count[Equal @@@ Transpose@{m1,m2}, True]] >Out[2]= {2.37 (2.43) Second, Null} >Out[3]= {3.63 (3.62) Second, 62479} >Out[4]= {3.59 (3.53) Second, 62479} > > >
- References:
- Re: style question
- From: Ray Koopman <koopman@sfu.ca>
- Re: style question