MathGroup Archive 2007

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

Search the Archive

Re: Map vs. Table

  • To: mathgroup at smc.vnet.net
  • Subject: [mg75392] Re: Map vs. Table
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Sat, 28 Apr 2007 05:54:19 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <f0sfrs$ncb$1@smc.vnet.net>

zac wrote:
> Dear Group,
> 
> consider a set of data (y values):
> 
> data = Table[i*j, {i, 5}, {j, 10}]
> 
> I want to label each Integer with an x value:
> 
> label := Table[{i, #} & /@ data[[i]], {i, Length[data]}]
> 
> I've understand that list creation is much faster with Map than with
> Table.
> Is there an effective way to convert the Table to Map in the label
> function (either with nested Map or to incorporate the Table function
> into the Map)?
> Would it be really faster for very large datasets than with Table?
> 
> thanks
> 
> Istvan Zachar
> 
> 

Istvan,

You will not, and cannot, get a straight or definite answer to this kind 
of question, because the performances depend on the size of the data set 
but also on its /structure/. You will find below some three functions: 
label, your original code, label2, a compile version of your original 
code, and label3, a function that uses MapIndexed to achieve the same 
functionality than your code (note that label3 is the slowest function 
in this test).

The first data set has few rows (5) and many columns (100,000). The 
compiled version is faster than the regular version.

The second data set has more rows (10) and still many columns (100,000). 
  label2 is still faster than label.

The third data set has many rows (100,000) and few columns (10). Both 
versions are on par. Note that label3 is even slower than during the 
precedent test.

In[1]:=
label:=Table[{i,#}&/@data[[i]],{i,Length[data]}]
label2=Compile[{{
       data,_Integer,2}},Table[{i,#}&/@data[[i]],{i,Length[data]}]];
label3:=Flatten[MapIndexed[{#2[[1]],#1}&,data,{2}],1]

In[4]:=
Timing[data=Table[i*j,{i,5},{j,10^5}];][[1]]
Timing[label; ][[1]]
Timing[label2[data]; ][[1]]
Timing[label3; ][[1]]

Out[4]=
0.531 Second

Out[5]=
0.109 Second

Out[6]=
0.094 Second

Out[7]=
1.328 Second

In[8]:=
Timing[data=Table[i*j,{i,10},{j,10^5}];][[1]]
Timing[label; ][[1]]
Timing[label2[data]; ][[1]]
Timing[label3; ][[1]]

Out[8]=
1.141 Second

Out[9]=
0.265 Second

Out[10]=
0.235 Second

Out[11]=
3. Second

In[12]:=
Timing[data=Table[i*j,{i,10^5},{j,10}];][[1]]
Timing[label; ][[1]]
Timing[label2[data]; ][[1]]
Timing[label3; ][[1]]

Out[12]=
0.235 Second

Out[13]=
0.265 Second

Out[14]=
0.266 Second

Out[15]=
3.344 Second

Regards,
Jean-Marc


  • Prev by Date: Re: Map vs. Table
  • Next by Date: Re: remote kernel
  • Previous by thread: Re: Map vs. Table
  • Next by thread: Re: Map vs. Table