Re: Manually nested Tables faster than builtin nesting?

• To: mathgroup at smc.vnet.net
• Subject: [mg113452] Re: Manually nested Tables faster than builtin nesting?
• From: Ray Koopman <koopman at sfu.ca>
• Date: Fri, 29 Oct 2010 06:28:37 -0400 (EDT)
• References: <ia8qh1\$1k1\$1@smc.vnet.net>

```On Oct 27, 2:14 am, Sebastian Schmitt <sschm... at physi.uni-
heidelberg.de> wrote:
> Dear all!
>
> I find that manually nested Tables are faster (factor 2 in my example)
> compared to the builtin nesting. I probably do a stupid thinko. Could
> you please have a look?
>
> In[52]:= imax = jmax = 5000;
>
> In[53]:= (tabletable = Table[Table[i*j, {i, imax}], {j, jmax}];) //
>   Timing // First
>
> Out[53]= 1.84
>
> In[54]:= (table = Table[i*j, {i, imax}, {j, jmax}];) // Timing // First
>
> Out[54]= 14.55
>
> In[55]:= tabletable == table
>
> Out[55]= True
>
> Thanks,
>
> Sebastian

The phenomenon seems to be version-dependent. Also, notice that I
have interchanged the loops in the two-table versions. To see why,
try both this order and the original order with {imax,jmax} = {4,6}.

\$Version
{imax,jmax} = {5000,5000}
Timing@Dimensions[t = Table[i+j,{i,imax},{j,jmax}]]
Timing@Dimensions[tt = Table[Table[i+j,{j,jmax}],{i,imax}]]
Timing@Dimensions[tu = Table[i+Table[j,{j,jmax}],{i,imax}]]
Timing@Dimensions[tv = Table[i+Range@jmax,{i,imax}]]
Timing@Dimensions[o = Outer[Plus,Range@imax,Range@jmax]]
Timing@Dimensions[a = Array[Plus,{imax,jmax}]]
SameQ[t,tt,tu,tv,o,a]

6.0 for Mac OS X PowerPC (32-bit) (June 19, 2007)
{5000, 5000}
{55.24, {5000, 5000}}
{6.13, {5000, 5000}}
{2.66, {5000, 5000}}
{2.25, {5000, 5000}}
{0.4, {5000, 5000}}
{0.39, {5000, 5000}}
True

5.2 for Mac OS X (June 20, 2005)
{5000,5000}
{4.38 Second,{5000,5000}}
{4.44 Second,{5000,5000}}
{3.06 Second,{5000,5000}}
{3.42 Second,{5000,5000}}
{17.66 Second,{5000,5000}}
{4.43 Second,{5000,5000}}
True

```

• Prev by Date: Re: Visualization for Dummies
• Next by Date: Re: How to make Mathematica automatically save a notebook
• Previous by thread: Re: Manually nested Tables faster than builtin nesting?
• Next by thread: Re: Manually nested Tables faster than builtin nesting?