RE: puzzling difference in speed

• To: mathgroup at smc.vnet.net
• Subject: [mg34713] RE: [mg34688] puzzling difference in speed
• From: "DrBob" <majort at cox-internet.com>
• Date: Sun, 2 Jun 2002 01:14:50 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```I have no idea why the compiler is so stubborn --- I don't understand
the little I know about Compile --- but that evidently explains your
other mystery.  That is, the timing of t2 and t5 are remarkably similar,
so it looks as if Mathematica is using compiled code there, but not in
t3 and t4.

Why does that happen?  For the same reason that comp1 and comp2 don't
compile.  (Whatever that is.)

n = 250;
t1 = Table[0.0, {n}, {n}, {n}];
Timing[t2 = Array[Plus, {n, n, n}];]
Timing[t3 = Array[Plus, Dimensions[t1]];]
Timing[dim = Dimensions[t1]]
Timing[t4 = Array[Plus, dim];]
Timing[t5 = comp3[t1];]

{1.797 Second, Null}
{19.906 Second, Null}
{0. Second, {200, 200, 200}}
{19.516 Second, Null}
{1.485 Second, Null}

The time for t5 was smaller than for t2 this time, but not always.
Here's another trial:

n = 250;
Timing[t1 = Table[0.0, {n}, {n}, {n}];]
Timing[Array[Plus, {n, n, n}];]
Timing[Array[Plus, Dimensions[t1]];]
Timing[comp3[t1];]

{2.078 Second, Null}
{2.031 Second, Null}
{18.657 Second, Null}
{2.047 Second, Null}

Bobby Treat

-----Original Message-----
From: Johannes Ludsteck
To: mathgroup at smc.vnet.net
[mailto:johannes.ludsteck at wiwi.uni-regensburg.de]
Subject: [mg34713] [mg34688] puzzling difference in speed

Dear MathGroup members,
is there anyone out who can explain why
computation of t2 (see below) is about 10 times faster than
computation of t3? (Note that t2 == t3).
The difference cannot be explained by the computation
time of Dimensions[t1], as Out[5] and Out[6] show.

Of course, the answer is not important for my work,
but the puzzling difference in computation time reduces
my confidence that Mathematica manages efficiency
problems of computation in a smart and *transparent* manner.

In[1]:= 	n=50;
In[2]:=		t1=Table[0.0,{n},{n},{n}];

In[3]:=		Timing[t2=Array[Plus,{n,n,n}];]
Out[3]=		{0.05 Second,Null}

In[4]:=		Timing[t3=Array[Plus,Dimensions[t1]];]
Out[4]=		{0.55 Second,Null}

In[5]:=		Timing[dim=Dimensions[t1]]
Out[5]=		{0. Second,{50,50,50}}

In[8]:=		Timing[t4=Array[Plus,dim];]
Out[8]=		{0.49 Second,Null}

By the way: if one tries to  compile the stuff, things
become even more strange:

In[7]:= comp1=Compile[{{t,_Real,3}},
Array[Plus,Dimensions[t]]]
Compile::cpiter: Iterator at position 2 of Array[Plus,Dimensions[t]]
cannot be compiled; evaluation will use the uncompiled function.

Why does Mathematica refute to compile the code?
Dimensions[t] must be a list of three integers,
since t is a rank 3 tensor. So try to help Mathematica:

In[8]:= comp2=Compile[{{t,_Real,3}},
With[{dim=Dimensions[t]},Array[Plus,dim]]]
Compile::cpiter: Iterator at position 2 of Array[Plus,dim] cannot be \
compiled; evaluation will use the uncompiled function.

Again Mathematica does not understand. So try to be more
patient and considerate with the compiler:

In[9]:= comp3=Compile[{{t,_Real,3}},
Block[{n1,n2,n3},
{n1,n2,n3}=Dimensions[t];
Array[Plus,{n1,n2,n3}]]]
Out[9] CompiledFunction[...]

At last now Mathematica does the job.

If you type Timing[compi[t1];]
for i in {1,2,3}, you will see that Mathematica is really
confused with comp1 and comp2, since it even reports external
evaluation errors.

Is there any straightforward explanation of such behaviour?

Best regards,
Johannes Ludsteck

<><><><><><><><><><><><>
Johannes Ludsteck
Economics Department
University of Regensburg
Universitaetsstrasse 31
93053 Regensburg
Phone +49/0941/943-2741

```

• Prev by Date: Generic Mathematica NonlinearRegress Question
• Next by Date: RE: Programming an algebraic experiment
• Previous by thread: puzzling difference in speed
• Next by thread: RE: puzzling difference in speed