Path: news.mathworks.com!not-for-mail
From: "Steve Amphlett" <Firstname.Lastname@Where-I-Work.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Why are cell arrays so much faster?
Date: Sat, 12 Jul 2008 14:24:02 +0000 (UTC)
Organization: Ricardo UK Ltd
Lines: 71
Message-ID: <g5aeq2$i3d$1@fred.mathworks.com>
References: <g58e4n$13t$1@fred.mathworks.com> <muy7ibsupgf.fsf@G99-Boettcher.llan.ll.mit.edu> <41094624-3981-4166-907d-d4d493b87466@8g2000hse.googlegroups.com>
Reply-To: "Steve Amphlett" <Firstname.Lastname@Where-I-Work.com>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1215872642 18541 172.30.248.38 (12 Jul 2008 14:24:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 12 Jul 2008 14:24:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 43398
Xref: news.mathworks.com comp.soft-sys.matlab:478981



NZTideMan <mulgor@gmail.com> wrote in message <41094624-
3981-4166-907d-d4d493b87466@8g2000hse.googlegroups.com>...
> On Jul 12, 8:37=A0am, Peter Boettcher 
<boettc...@ll.mit.edu> wrote:
> > "David Doria" <daviddo...@gmail.com> writes:
> > > Below is my experiment - I actually expected the 
opposite!
> >
> > >>> tic; CubeA(1:300, 1:360, 1) =3D repmat
(1000,300,360); toc
> > > Elapsed time is 0.003197 seconds.
> > >>> tic; CubeB{1} =3D repmat(1000,300,360); toc
> > > Elapsed time is 0.000964 seconds.
> >
> > > Why is this the case? =A0
> >
> > A cell array element is for all practical purposes the 
same as a
> > separate variable. =A0So really this is just the 
difference between
> >
> > CubeA(1:300, 1:360, 1) =3D repmat(1000,300,360);
> >
> > and
> >
> > CubeB =3D repmat(1000,300,360);
> >
> > The first includes array indices on the left side, so 
it includes
> > subarray assignment into the CubeA variable. =A0The 
second simply names
> > the output from repmat.
> >
> > I can't predict or guess at how the just-in-time 
compiler or the
> > accelerator will react to the first line. =A0MATLAB 
might actually incur
> > the cost of allocating two arrays of the same size, one 
for a new
> > variable called CubeA, and the other for the temporary 
output of repmat,
> > and a memcpy to do the subarray assignment. =A0In the 
second case, no suc=
> h
> > thing would ever happen.
> >
> > -Peter
> 
> But how accurate is tic/toc?
> You're measuring the difference between 3 milliseconds 
and 0.9
> milliseconds.
> A better test would be to put the commands within a large 
for/end
> loop, say 10000 iterations:
> 
> >> tic;for it=3D1:10000,CubeA(1:300, 1:360, 1) =3D repmat
(1000,300,360);end=
> ,toc
> 
> Elapsed time is 30.724523 seconds.
> 
> >> tic;for it=3D1:10000,CubeB{1} =3D repmat
(1000,300,360);end,toc
> 
> Elapsed time is 19.932247 seconds.
> 
> Now I believe it.

One piece is still missing though.  The OP never said 
whether his CubeA was pre-assigned.