Re: puzzling difference in speed
- To: mathgroup at smc.vnet.net
- Subject: [mg34821] Re: puzzling difference in speed
- From: "Carl K. Woll" <carlw at u.washington.edu>
- Date: Sat, 8 Jun 2002 05:21:39 -0400 (EDT)
- References: <200206040741.DAA03886@smc.vnet.net> <adpfh5$j45$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Johannes and all, The reason for the difference in speed is the use of packed arrays. Repeating Fred Simon's form of the original example: In[22]:= n=50;t1=Table[0,{n},{n},{n}]; In[23]:= t=array[Plus,Dimensions[t1]]; u=array[Plus,{n,n,n}]; t===u Out[25]= True So, as far as we can tell, t and u are the same. But, when we apply Array to each of the expressions we get: In[26]:= Apply[Array,t];//Timing Apply[Array,u];//Timing Out[26]= {0.14 Second, Null} Out[27]= {0.016 Second, Null} So, t and u must not be the same, since the timings are so different. Let's see if PackedArrays are being used: In[28]:= Developer`PackedArrayForm[t] Developer`PackedArrayForm[u] Out[28]= array[Plus, PackedArray[Integer, <3>]] Out[29]= array[Plus, {50, 50, 50}] Now we see the culprit, t and u aren't really identical, as t uses PackedArrays and u doesn't. In this particular case, having packed arrays is bad, so try instead: In[32]:= Apply[Array,Developer`FromPackedArray[t]];//Timing Apply[Array,u];//Timing Out[32]= {0.031 Second, Null} Out[33]= {0.016 Second, Null} You still might be puzzled as to why the packed array case is so slow. The answer is a bit paradoxical. In the case where the dimensions of the array are strictly integers, Mathematica looks at the size of the matrix to be produced. If the matrix is sufficiently large, and if only machine numbers are involved, then Mathematica's packed array technology kicks in and the answer is produced very quickly. On the other hand, if a packed array is present, that is the dimensions of the array is a packed array, then Mathematica doesn't use the packed array technology to compute the result. Consider In[61]:= Apply[Array,t]//Developer`PackedArrayQ Out[61]= False In[62]:= Apply[Array,u]//Developer`PackedArrayQ Out[62]= True So, the moral of the story is that packed arrays can effect your results in surprising ways. Carl Woll Physics Dept U of Washington ----- Original Message ----- From: "Fred Simons" <f.h.simons at tue.nl> To: mathgroup at smc.vnet.net Subject: [mg34821] Re: puzzling difference in speed > Hartmut Wolf remarked with respect to the given examples:: > > > Obviously the computing machinery for Array behaves differently when the > > dimensions are given explicitly or introduced as an expression (to be > > evaluated) > > > > > It seems to be more complicated. Have a look at the following results: > > In[1]:= > n=100; t1 = Table[0, {n},{n},{n}]; > > In[2]:= > t = array[Plus, Dimensions[t1]]; > u = array[Plus, {100, 100, 100}]; > Equal[t, u] > > Out[4]= True > > In[5]:= > ReplaceAll[t, array\[Rule]Array]; // Timing > ReplaceAll[u ,array\[Rule]Array]; // Timing > > Out[5]= > {0.65 Second,Null} > Out[6]= > {0.77 Second,Null} > > In[7]:= > Apply[Array, t]; // Timing > Apply[Array, u]; // Timing > > Out[7]= > {4.51 Second,Null} > Out[8]= > {0.72 Second,Null} > > Despite the fact that t equals u, we have the same difference in timing. > Does Mathematica 'remember' the way the expression t has been computed? > > Fred Simons > Eindhoven University of Technology > > >
- References:
- RE: puzzling difference in speed
- From: "Wolf, Hartmut" <Hartmut.Wolf@t-systems.com>
- RE: puzzling difference in speed