Path: news.mathworks.com!newsfeed-00.mathworks.com!panix!bloom-beacon.mit.edu!llnews!53ab2750!not-for-mail
From: Peter Boettcher <boettcher@ll.mit.edu>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Why are cell arrays so much faster?
References: <g58e4n$13t$1@fred.mathworks.com>
Message-ID: <muy7ibsupgf.fsf@G99-Boettcher.llan.ll.mit.edu>
Organization: MIT Lincoln Laboratory
User-Agent: Gnus/5.110006 (No Gnus v0.6) Emacs/23.0.0 (gnu/linux)
Cancel-Lock: sha1:dmOW9y/bWSK9vnn6GMR5xUUaeNw=
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Lines: 33
Date: Fri, 11 Jul 2008 16:37:52 -0400
NNTP-Posting-Host: 155.34.163.114
X-Complaints-To: news@ll.mit.edu
X-Trace: llnews 1215807944 155.34.163.114 (Fri, 11 Jul 2008 16:25:44 EDT)
NNTP-Posting-Date: Fri, 11 Jul 2008 16:25:44 EDT
Xref: news.mathworks.com comp.soft-sys.matlab:478933



"David Doria" <daviddoria@gmail.com> writes:

> Below is my experiment - I actually expected the opposite!
>
>>> tic; CubeA(1:300, 1:360, 1) = repmat(1000,300,360); toc
> Elapsed time is 0.003197 seconds.
>>> tic; CubeB{1} = repmat(1000,300,360); toc
> Elapsed time is 0.000964 seconds.
>
> Why is this the case?  

A cell array element is for all practical purposes the same as a
separate variable.  So really this is just the difference between

CubeA(1:300, 1:360, 1) = repmat(1000,300,360);

and

CubeB = repmat(1000,300,360);


The first includes array indices on the left side, so it includes
subarray assignment into the CubeA variable.  The 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.  MATLAB 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.  In the second case, no such
thing would ever happen.

-Peter