Re: unexpected Timing results
- To: mathgroup at smc.vnet.net
- Subject: [mg115839] Re: unexpected Timing results
- From: Arnoud Buzing <arnoudb at wolfram.com>
- Date: Sat, 22 Jan 2011 03:22:48 -0500 (EST)
On 1/21/11 3:35 AM, gleam at flashmail.com wrote:
> I experienced a couple of rather unexpected timing results today that
> I am hoping can be explained to me.
>
> In Mathematica 7.0.1:
>
> ragged = RandomInteger[1*^6, #]&~Array~1000;
>
> Do[PadLeft[ragged], {50}] // Timing
> Do[PadLeft[ragged, {1000, 1000}], {50}] // Timing
> Do[PadLeft[#, 1000]& /@ ragged, {50}] // Timing
>
> {4.422, Null}
> {4.406, Null}
> {0.344, Null}
>
> This difference I did not anticipate. I suppose this has something
> to do with packed arrays, but I do not understand why the upper two
> forms do not handle the task at least as well as the third one.
Before running your experiment, you can evaluate:
On["Packing"]
to see when expressions are being unpacked.
The 'ragged' expression is a list of packed arrays, but the whole
'ragged' expression is not packed:
In[16]:= Union[Developer`PackedArrayQ /@ ragged]
Out[16]= {True}
In[17]:= Developer`PackedArrayQ[ragged]
Out[17]= False
(The reason for this is that ragged arrays can not be packed).
So the 'ragged' expression is like: { <packed_array>, <packed_array>, ... }
The first two Do[] loops are therefore unpacking the whole expression,
to run the normal (non-packed, slow) evaluation.
The last Do[] loop maps over individual packed arrays, and evaluates
with the packed evaluation code.