puzzling difference in speed
- To: mathgroup at smc.vnet.net
- Subject: [mg34688] puzzling difference in speed
- From: "Johannes Ludsteck" <johannes.ludsteck at wiwi.uni-regensburg.de>
- Date: Sat, 1 Jun 2002 04:28:49 -0400 (EDT)
- Organization: Universitaet Regensburg
- Sender: owner-wri-mathgroup at wolfram.com
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