Re: Product
- To: mathgroup at smc.vnet.net
- Subject: [mg87592] Re: Product
- From: Szabolcs Horvát <szhorvat at gmail.com>
- Date: Mon, 14 Apr 2008 05:41:36 -0400 (EDT)
- Organization: University of Bergen
- References: <200804110542.BAA04653@smc.vnet.net> <200804121059.GAA00044@smc.vnet.net> <ftsd2p$bca$1@smc.vnet.net>
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,
Unprotect[Apply]
Apply[Plus, arr_?Developer`PackedArrayQ] := Total[arr]
Protect[Apply]
(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.)
- Follow-Ups:
- Re: Re: Product
- From: Andrzej Kozlowski <akoz@mimuw.edu.pl>
- Re: Re: Product
- References:
- Product
- From: "Steven" <steven_504@telenet.be>
- RE: Product
- From: "Jose Luis Gomez" <jose.luis.gomez@itesm.mx>
- Product