MathGroup Archive 2007

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

Search the Archive

Re: Map vs. Table

  • To: mathgroup at smc.vnet.net
  • Subject: [mg75422] Re: Map vs. Table
  • From: Szabolcs <szhorvat at gmail.com>
  • Date: Sun, 29 Apr 2007 03:13:55 -0400 (EDT)
  • Organization: University of Bergen
  • References: <f0v6d5$91v$1@smc.vnet.net>

Bill Rowe wrote:
> On 4/27/07 at 5:25 AM, replicatorzed at gmail.com (zac) wrote:
> 
>> consider a set of data (y values):
> 
>> data = Table[i*j, {i, 5}, {j, 10}]
> 
> The same data can be generated more quickly with Outer. That is:
> 
> In[15]:=
> Outer[Times,Range@5,Range@10]==Table[i*j,{i,5},{j,10}]
> 

Just a note about this:

I would also have chosen Outer to generate data, but for some reason 
this doesn't give a packed array.

In[5]:=
data=Outer[Times,Range@1000,Range@1000];

In[6]:=
PackedArrayQ@data

Out[6]=
False

> 
> and
> 
> In[16]:=
> Timing[Do[Table[i*j,{i,5},{j,10}],{10000}];]
> 
> Out[16]=
> {0.947914 Second,Null}
> 
> while
> 
> In[17]:=
> Timing[Do[Outer[Times,Range@5,Range@10],{10000}];]
> 
> Out[17]=
> {0.451616 Second,Null}
> 

Yes, but it also depends on the size of the matrix.

In[10]:=
Timing[Do[Table[i j,{i,1000},{j,1000}],{20}];]

Out[10]=
{2.125 Second,Null}

In[11]:=
Timing[Do[Outer[Times,Range@1000,Range@1000],{20}];]

Out[11]=
{8.391 Second,Null}


I hope that these deficiencies of Outer and Inner (i.e that they don't 
work with packed arrays) will be corrected in a future Mathematica.

As Jean-Marc's reply also demonstrates, it is not always easy to predict 
the efficiency of different approaches. Are there any published 
guidelines available on how to write efficient code in Mathematica?

Szabolcs


  • Prev by Date: Re: Porting waveforms out of Mathematica and into a D/A converter
  • Next by Date: Re: associate a parameter name with a string
  • Previous by thread: Re: Map vs. Table
  • Next by thread: Reply about Piecewise and Integral