MathGroup Archive 2008

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

Search the Archive

Re: Product

Andrzej Kozlowski wrote:
> The reason for the speed of Total unlike Plus is that it does not need  
> to unpack packed arrays. Here is how you can see what happens.
> SetSystemOptions["PackedArrayOptions" -> {"UnpackMessage" -> True}];
> mytestlist = Table[RandomInteger[{1, 9}], {1000000}];
> Developer`PackedArrayQ[mytestlist]
> True
> So mytestlist is a packed array. Now let's see what happens if we add  
> it up using Plus:
> In[4]:= Plus @@ mytestlist
> During evaluation of In[4]:= Developer`FromPackedArray::"unpack1" :  
> "Unpacking array."
> During evaluation of In[4]:= Developer`FromPackedArray::"punpack1" :  
> "Unpacking array to
> Out[4]= 4998322
> Now the same with Total:
> In[5]:= Total[mytestlist]
> Out[5]= 4998322
> It is the unpacking of packed arrays by Plus that makes adding up this  
> list using Plus much slower.

But of course in principle there is nothing that would make it 
impossible to introduce a special case for functions like Times and 
Plus, and make them work with packed arrays.  So this alone does not 
make it necessary to introduce a new syntax/new function, like Total[].

For example,

Apply[Plus, arr_?Developer`PackedArrayQ] := Total[arr]

(Of course this is just an example to show that it is possible to do 
this.  It is not a complete implementation and certainly not something 
that I would recommend for _users_ to do.  But it would be nice if WRI 
provided these special case algorithms unless there is some limitation 
in Mathematica's internal design that makes it difficult/impossible to 
do this in a proper way.)

  • References:
    • Product
      • From: "Steven" <>
    • RE: Product
      • From: "Jose Luis Gomez" <>
  • Prev by Date: Re: List concatenation speed
  • Next by Date: Re: List concatenation speed
  • Previous by thread: Re: RE: Product
  • Next by thread: Re: Re: Product