MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: style question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg79529] Re: style question
  • From: Ray Koopman <koopman at sfu.ca>
  • Date: Sat, 28 Jul 2007 05:41:53 -0400 (EDT)
  • References: <f84jp8$qmp$1@smc.vnet.net>

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?

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}



  • Prev by Date: Re: Love and Tensor Algebra
  • Next by Date: Re: Re: Re: Locator question
  • Previous by thread: Re: Re: style question
  • Next by thread: Re: Re: style question