Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Preallocation in MATLAB R2009a
Date: Wed, 5 Aug 2009 17:52:02 +0000 (UTC)
Organization: &#28145;&#22323;&#36808;&#29790;&#29983;&#29289;&#21307;&#30103;&#30005;&#23376;&#32929;&#20221;&#26377;&#38480;&#20844;&#21496;
Lines: 33
Message-ID: <h5cgs2$52$1@fred.mathworks.com>
References: <h5c475$639$1@fred.mathworks.com> <h5c9nq$fsm$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1249494722 162 172.30.248.35 (5 Aug 2009 17:52:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 5 Aug 2009 17:52:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1101180
Xref: news.mathworks.com comp.soft-sys.matlab:561080


"qooroo " <qooroo@gmail.com> wrote in message <h5c9nq$fsm$1@fred.mathworks.com>...
> "rocwoods" <rocwoods@yeah.net> wrote in message <h5c475$639$1@fred.mathworks.com>...
> > It's known that to use a matrix variable "a" with changeless size m,n,we should preallocate memory for the variable to achieve good performance.Usually,We use "a= zeros(m,n)" to preallocate.
> > But I find in R2009a using "a(m,n) = 0" to preallocate is much fast than "a = zeros(m,n)",especially with large m,n. The following shows this:
> > >> clear
> > >> tic;a = zeros(10000,11000);toc
> > Elapsed time is 1.054111 seconds.
> > >> clear
> > >> tic;a (10000,11000) = 0;toc
> > Elapsed time is 0.000049 seconds.
> > 
> > I think in R2009a,"a(m,n) = 0" do NOT really assign each element of a with 0,but just allocate a certain zone in memory for a.When you visit a certain element of a,the assignment happens,  is that right? Thanks to all.
> > 
> > Bye the way,
> > My computer's  disposition:  Intel Core2 Duo CPU T8100 @ 2.1GHz 2.1GHz,   2G memory .
> 
> I don't think so:
> 
> >>a1 = zeros(10000,11000);
> >>a2(10000,11000) = 0;
> >>whos
>   Name         Size                Bytes  Class     Attributes
> 
>   a         10000x11000            880000000 double              
>   a2        10000x11000            880000000 double  
> 
> I'm guessing the overhead comes from whatever else is in the "zeros" function.
> 
> -qooroo

Thank U very much&#65281;
Maybe I didn't make it clear, my puzzle is what result in the  huge speed difference between "a = zeros(m,n)" and "a(m,n)= 0",since zeros is a built-in function and has good efficience.
I'm guessing "a(m,n) = 0" just do a few things like labeling the beginning and end of the variable in memory, since free memory space are all 0s.