Path: news.mathworks.com!newsfeed-00.mathworks.com!nlpi057.nbdc.sbc.com!prodigy.net!nx01.iad01.newshosting.com!newshosting.com!198.186.194.249.MISMATCH!transit3.readnews.com!news-out.readnews.com!news-xxxfer.readnews.com!postnews.google.com!8g2000hse.googlegroups.com!not-for-mail
From: NZTideMan <mulgor@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Why are cell arrays so much faster?
Date: Fri, 11 Jul 2008 14:03:32 -0700 (PDT)
Organization: http://groups.google.com
Lines: 50
Message-ID: <41094624-3981-4166-907d-d4d493b87466@8g2000hse.googlegroups.com>
References: <g58e4n$13t$1@fred.mathworks.com> <muy7ibsupgf.fsf@G99-Boettcher.llan.ll.mit.edu>
NNTP-Posting-Host: 202.78.152.105
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
X-Trace: posting.google.com 1215810212 29889 127.0.0.1 (11 Jul 2008 21:03:32 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Fri, 11 Jul 2008 21:03:32 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: 8g2000hse.googlegroups.com; posting-host=202.78.152.105; 
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon; 
X-HTTP-Via: 1.1 nc4 (NetCache NetApp/6.0.5P1)
Xref: news.mathworks.com comp.soft-sys.matlab:478934



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.