Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Preallocation in MATLAB R2009a

Subject: Preallocation in MATLAB R2009a

From: rocwoods

Date: 5 Aug, 2009 14:16:05

Message: 1 of 31

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 .

Subject: Preallocation in MATLAB R2009a

From: qooroo

Date: 5 Aug, 2009 15:50:18

Message: 2 of 31

"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

Subject: Preallocation in MATLAB R2009a

From: Oleg Komarov

Date: 5 Aug, 2009 16:08:02

Message: 3 of 31

"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 .

R2008b:
tic
    a = zeros(10000,10000);
toc
tic
    b(10000,10000) = 0;
toc
isequal(a,b)

Elapsed time is 0.493878 seconds.
Elapsed time is 0.002202 seconds.
ans = 1

BTW is it better to preallocate zeros or NaN?

Subject: Preallocation in MATLAB R2009a

From: someone

Date: 5 Aug, 2009 16:16:02

Message: 4 of 31

"Oleg Komarov" <oleg.komarov@hotmail.it> wrote in message <h5cap2$6e9$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 .
>
> R2008b:
> tic
> a = zeros(10000,10000);
> toc
> tic
> b(10000,10000) = 0;
> toc
> isequal(a,b)
>
> Elapsed time is 0.493878 seconds.
> Elapsed time is 0.002202 seconds.
> ans = 1
>
> BTW is it better to preallocate zeros or NaN?

% Depends on how you use it in your code.

% FYI:
>> clear all
>> a1 = ones(100,110);
>> a2(100,110) = 1;
>> isequal(a1,a2)

ans =

     0

>>

Subject: Preallocation in MATLAB R2009a

From: someone

Date: 5 Aug, 2009 17:14:05

Message: 5 of 31

"Oleg Komarov" <oleg.komarov@hotmail.it> wrote in message <h5cap2$6e9$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 .
>
> R2008b:
> tic
> a = zeros(10000,10000);
> toc
> tic
> b(10000,10000) = 0;
> toc
> isequal(a,b)
>
> Elapsed time is 0.493878 seconds.
> Elapsed time is 0.002202 seconds.
> ans = 1

I would like to make one observation here.
If you assume that b in the above code will
result in matrix filled with zeros, then you are
relying on an "undocumented" feature.

Whos to say what might happen in future MATLAB releases?
Maybe b will be filled with NaNs execpt for b(10000,10000).

>
> BTW is it better to preallocate zeros or NaN?

Subject: Preallocation in MATLAB R2009a

From: Oleg Komarov

Date: 5 Aug, 2009 17:32:18

Message: 6 of 31

"someone" <someone@somewhere.net> wrote in message <h5cekt$3nt$1@fred.mathworks.com>...
> "Oleg Komarov" <oleg.komarov@hotmail.it> wrote in message <h5cap2$6e9$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 .
> >
> > R2008b:
> > tic
> > a = zeros(10000,10000);
> > toc
> > tic
> > b(10000,10000) = 0;
> > toc
> > isequal(a,b)
> >
> > Elapsed time is 0.493878 seconds.
> > Elapsed time is 0.002202 seconds.
> > ans = 1
>
> I would like to make one observation here.
> If you assume that b in the above code will
> result in matrix filled with zeros, then you are
> relying on an "undocumented" feature.
>
> Whos to say what might happen in future MATLAB releases?
> Maybe b will be filled with NaNs execpt for b(10000,10000).
>
> >
> > BTW is it better to preallocate zeros or NaN?

a = ones(2,2)
b(2,2) = 1
a =
     1 1
     1 1
b =
     0 0
     0 1

Lets say now it makes sense. has Yair Altman ever mentioned this?

Subject: Preallocation in MATLAB R2009a

From: Steven Lord

Date: 5 Aug, 2009 17:33:24

Message: 7 of 31


"someone" <someone@somewhere.net> wrote in message
news:h5cekt$3nt$1@fred.mathworks.com...

*snip*

> I would like to make one observation here.
> If you assume that b in the above code will
> result in matrix filled with zeros, then you are
> relying on an "undocumented" feature.
>
> Whos to say what might happen in future MATLAB releases?
> Maybe b will be filled with NaNs execpt for b(10000,10000).

Actually, this behavior is documented. Look at the last example in the
"Adding Smaller Blocks to a Matrix" subsection on this page from the
documentation:

http://www.mathworks.com/access/helpdesk/help/techdoc/math/f1-85766.html

Changing the "fill in" value would be a MAJOR backwards incompatibility, so
it's highly unlikely we'd make that change. If we did decide to make a
change like that, it would be called out in bold red flashing letters in the
Release Notes, in several sections of the documentation, and probably in
several other ways.

--
Steve Lord
slord@mathworks.com

Subject: Preallocation in MATLAB R2009a

From: Steven Lord

Date: 5 Aug, 2009 17:36:42

Message: 8 of 31


"Oleg Komarov" <oleg.komarov@hotmail.it> wrote in message
news:h5cfn2$dh7$1@fred.mathworks.com...

*snip*

> a = ones(2,2)
> b(2,2) = 1
> a =
> 1 1
> 1 1
> b =
> 0 0
> 0 1
>
> Lets say now it makes sense. has Yair Altman ever mentioned this?

Why would he? If you assign a value to an element beyond the bounds of the
matrix, the elements that need to be created to perform that assignment
while keeping the matrix rectangular are filled in with 0's. Note the
difference between:

a = ones(2, 2)
b1(2, 2) = 1

and:

a = ones(2, 2)
b2(1:2, 1:2) = 1

--
Steve Lord
slord@mathworks.com

Subject: Preallocation in MATLAB R2009a

From: Oleg Komarov

Date: 5 Aug, 2009 17:41:03

Message: 9 of 31

"Steven Lord" <slord@mathworks.com> wrote in message <h5cfv8$hl$1@fred.mathworks.com>...
>
> "Oleg Komarov" <oleg.komarov@hotmail.it> wrote in message
> news:h5cfn2$dh7$1@fred.mathworks.com...
>
> *snip*
>
> > a = ones(2,2)
> > b(2,2) = 1
> > a =
> > 1 1
> > 1 1
> > b =
> > 0 0
> > 0 1
> >
> > Lets say now it makes sense. has Yair Altman ever mentioned this?
>
> Why would he? If you assign a value to an element beyond the bounds of the
> matrix, the elements that need to be created to perform that assignment
> while keeping the matrix rectangular are filled in with 0's. Note the
> difference between:
>
> a = ones(2, 2)
> b1(2, 2) = 1
>
> and:
>
> a = ones(2, 2)
> b2(1:2, 1:2) = 1
>
> --
> Steve Lord
> slord@mathworks.com
>
I feel like a beginner. Thanks for the reference S. Lord!

Subject: Preallocation in MATLAB R2009a

From: rocwoods

Date: 5 Aug, 2009 17:52:02

Message: 10 of 31

"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!
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.

Subject: Preallocation in MATLAB R2009a

From: Bruno Luong

Date: 5 Aug, 2009 18:08:04

Message: 11 of 31


>
> Thank U very much!
> 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.

No, I don't believe. My theory is simply poor programming of ZEROS function. Oh well. Good to know though.

Bruno

Subject: Preallocation in MATLAB R2009a

From: someone

Date: 5 Aug, 2009 18:15:21

Message: 12 of 31

"Steven Lord" <slord@mathworks.com> wrote in message <h5cfp2$hes$1@fred.mathworks.com>...
>
> "someone" <someone@somewhere.net> wrote in message
> news:h5cekt$3nt$1@fred.mathworks.com...
>
> *snip*
>
> > I would like to make one observation here.
> > If you assume that b in the above code will
> > result in matrix filled with zeros, then you are
> > relying on an "undocumented" feature.
> >
> > Whos to say what might happen in future MATLAB releases?
> > Maybe b will be filled with NaNs execpt for b(10000,10000).
>
> Actually, this behavior is documented. Look at the last example in the
> "Adding Smaller Blocks to a Matrix" subsection on this page from the
> documentation:
>
> http://www.mathworks.com/access/helpdesk/help/techdoc/math/f1-85766.html
>
> Changing the "fill in" value would be a MAJOR backwards incompatibility, so
> it's highly unlikely we'd make that change. If we did decide to make a
> change like that, it would be called out in bold red flashing letters in the
> Release Notes, in several sections of the documentation, and probably in
> several other ways.
>
> --
> Steve Lord
> slord@mathworks.com
>

Nice to know, didn't realize that!
But then again, there IS a difference between
what is "highly unlikely" and what is "guaranteed".
It would be VERY disturbing if zeros(m,n) returned
anything except a matrix filled with zeros.

Subject: Preallocation in MATLAB R2009a

From: us

Date: 5 Aug, 2009 18:22:20

Message: 13 of 31

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h5chq4$3nt$1@fred.mathworks.com>...
>
> >
> > Thank U very much!
> > 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.
>
> No, I don't believe. My theory is simply poor programming of ZEROS function. Oh well. Good to know though.
>
> Bruno

nah... just call it ...correctly...

% clear all; % <- use this for proper testing...
     clear r; % <- save old stuff!
     n=5000;
tic;
     r(ones(n))=0;
toc
    clear r;
tic;
     r=zeros(n);
toc
     clear r;
tic
     r=zeros(n,'double');
toc
% results on a wintel sys: ic2/2*2.6gzh/2gb/winxp32.sp3/r2009a
% Elapsed time is 0.646796 seconds. ONES
% Elapsed time is 0.163560 seconds. ZEROS
% Elapsed time is 0.001191 seconds. ZEROS(...,'double')

us

Subject: Preallocation in MATLAB R2009a

From: us

Date: 5 Aug, 2009 18:38:06

Message: 14 of 31

"us "

the previous reply contains an unbelievably STUPID error...
here's the ...corrected... version

% clear all; % <- use this for proper testing...
     clear r; % <- save old stuff!
     n=5000;
tic;
     r(n,n)=0;
toc
     clear r;
tic;
     r=zeros(n);
toc
     clear r;
tic
     r=zeros(n,'double');
toc
     clear r;
% results on a wintel sys: ic2/2*2.6gzh/2gb/winxp32.sp3/r2009a
% after clear all;
% Elapsed time is 0.000046 seconds. ONES()
% Elapsed time is 0.139465 seconds. ZEROS()
% Elapsed time is 0.000054 seconds. ZEROS(...,'double')

terribly sorry for confusion
us

Subject: Preallocation in MATLAB R2009a

From: Bruno Luong

Date: 5 Aug, 2009 18:47:03

Message: 15 of 31

"us " <us@neurol.unizh.ch> wrote in message <h5cikr$jh$1@fred.mathworks.com>...

> tic;
> r=zeros(n);
> toc

It does take much effort for Mathworks to treat *any* calling in a better way. Frankly, who think about specify class all the time when ZEROS is invoked? Certainly not me, and I must have thousands of unspecified-class zeros calls in my Matlab folder. Something like this would be a simple work around.

function z = zeros(varargin)
if ~ischar(varargin{end})
   z = builtin('zeros', varargin{:}, 'double');
else
   z = builtin('zeros', varargin{:});
end

I suspect the slow speed is not due to overhead, but the built-in implementation does not use something like C MEMSET to fill the memory. They must add improve this function for the next release.

Bruno

Subject: Preallocation in MATLAB R2009a

From: Yair Altman

Date: 5 Aug, 2009 19:20:04

Message: 16 of 31

BTW, this is also the case in older Matlab versions, not just 2009a or 2008b.

And to the OP who asked, thanks for the compliment but I'm afraid I was unaware of these nuances until now. I live and learn (not in the least from CSSM threads like this)...

Yair Altman
http://UndocumentedMatlab.com
 

Subject: Preallocation in MATLAB R2009a

From: Matt Fig

Date: 5 Aug, 2009 19:36:04

Message: 17 of 31

"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <h5cm14$i3d$1@fred.mathworks.com>...
> BTW, this is also the case in older Matlab versions, not just 2009a or 2008b.


I see virtually no difference in 2007a.

clear all
n = 5000;

tic;
r(n,n) = 0;
toc

clear all;
n = 5000;

tic;
r = zeros(n);
toc

clear all;
n = 5000;

tic
r = zeros(n,'double');
toc

Elapsed time is 0.369892 seconds.
Elapsed time is 0.374925 seconds.
Elapsed time is 0.372524 seconds.

Subject: Preallocation in MATLAB R2009a

From: us

Date: 5 Aug, 2009 19:52:02

Message: 18 of 31

"Matt Fig" <spamanon@yahoo.com> wrote in message <h5cmv3$lb5$1@fred.mathworks.com>...
> "Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <h5cm14$i3d$1@fred.mathworks.com>...
> > BTW, this is also the case in older Matlab versions, not just 2009a or 2008b.
>
>
> I see virtually no difference in 2007a.
> Elapsed time is 0.369892 seconds.
> Elapsed time is 0.374925 seconds.
> Elapsed time is 0.372524 seconds.

- AND - your os/architecture is...

:-)
us

Subject: Preallocation in MATLAB R2009a

From: Matt Fig

Date: 5 Aug, 2009 20:24:01

Message: 19 of 31

"us " <us@neurol.unizh.ch> wrote in message <h5cnt2$n8p$1@fred.mathworks.com>...
> - AND - your os/architecture is...
>
> :-)
> us

Oh yes:

Win Vista 32-bit, Intel Core 2 @2.33 GHz, 4 GB RAM

Subject: Preallocation in MATLAB R2009a

From: James Tursa

Date: 5 Aug, 2009 20:32:03

Message: 20 of 31

I just ran some tests on WinXP R2006a through R2009a, and the difference first shows up in R2008b. I recalled that some time ago I compared building a zero matrix from scratch in a C-mex file vs the MATLAB intrinsic zeros function and got essentially the same timing result. That was back in R2007a. But now when I run the same test in R2009a I get the same huge speed difference that is showing up with the a(10000,11000)=0 method. The C-mex routine used mxCalloc to get the memory. I also tried mxMalloc and got essentially the same result. So the underlying methods used for mxMalloc and mxCalloc appear to have changed from R2008a to R2008b. Maybe MATLAB now keeps better track of which memory is already zeroed out, so calls to mxCalloc or the like don't always have to do this work because it has already been done previously. Just a guess. Seems like the zeros function is not taking advantage
of that, where the a(etc,etc)=0 method does.

James Tursa

Subject: Preallocation in MATLAB R2009a

From: Nathan

Date: 5 Aug, 2009 20:35:10

Message: 21 of 31

On Aug 5, 12:36 pm, "Matt Fig" <spama...@yahoo.com> wrote:
> "Yair Altman" <altmany...@gmailDEL.comDEL> wrote in message <h5cm14$i3...@fred.mathworks.com>...
> > BTW, this is also the case in older Matlab versions, not just 2009a or 2008b.
>
> I see virtually no difference in 2007a.
>
> clear all
> n = 5000;
>
> tic;
> r(n,n) = 0;
> toc
>
> clear all;
> n = 5000;
>
> tic;
> r = zeros(n);
> toc
>
> clear all;
> n = 5000;
>
> tic
> r = zeros(n,'double');
> toc
>
> Elapsed time is 0.369892 seconds.
> Elapsed time is 0.374925 seconds.
> Elapsed time is 0.372524 seconds.

Just for kicks:
%%%%%%%%%%%%%%%%%%%%%%%%%%%
Matlab r2007b:
clear all
n = 5000;
tic;
r(n,n) = 0;
toc
clear all;
n = 5000;
tic;
r = zeros(n);
toc
clear all;
n = 5000;
tic
r = zeros(n,'double');
toc
Elapsed time is 0.112516 seconds.
Elapsed time is 0.110922 seconds.
Elapsed time is 0.114900 seconds.
%%%%%%%%%%%%%%%%%%%%%%%%%%%

Matlab r2009a:
clear all
n = 5000;
tic;
r(n,n) = 0;
toc
clear all;
n = 5000;
tic;
r = zeros(n);
toc
clear all;
n = 5000;
tic
r = zeros(n,'double');
toc
Elapsed time is 0.000595 seconds.
Elapsed time is 0.114555 seconds.
Elapsed time is 0.000891 seconds.
%%%%%%%%%%%%%%%%%%%%%%%%%%%

Winxp 32-bit sp2, Intel Core 2, @3.33GHz 3.32 GHz, 3.25 GB of RAM

Looks like Yair wasn't quite correct?
-Nathan

Subject: Preallocation in MATLAB R2009a

From: James Tursa

Date: 5 Aug, 2009 20:43:03

Message: 22 of 31

On a related note, I previously had tested the following (R2007a??) in a C-mex routine:

- Get a huge memory block with mxMalloc
- Fill it with non-zero data
- Free it with mxFree
- Immediately get the huge block again and verify that the address is the same
- Examine the contents

The results were that the memory block was zeroed out. So either the mxFree call, or the mxMalloc call, or something inbetween was zeroing out the memory. I will see if I can find that test and rerun it again ...

James Tursa

Subject: Preallocation in MATLAB R2009a

From: Bruno Luong

Date: 5 Aug, 2009 20:58:03

Message: 23 of 31

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <h5cq83$7o$1@fred.mathworks.com>...
> Just a guess. Seems like the zeros function is not taking advantage
> of that, where the a(etc,etc)=0 method does.

But James, this does not explain the difference of speed when zeros() is invokes with and without specified classes as us has showed.

Bruno

Subject: Preallocation in MATLAB R2009a

From: us

Date: 5 Aug, 2009 20:59:05

Message: 24 of 31

"us " <us@neurol.unizh.ch> wrote in message <h5cnt2$n8p$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message <h5cmv3$lb5$1@fred.mathworks.com>...
> > "Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <h5cm14$i3d$1@fred.mathworks.com>...
> > > BTW, this is also the case in older Matlab versions, not just 2009a or 2008b.
> >
> >
> > I see virtually no difference in 2007a.
> > Elapsed time is 0.369892 seconds.
> > Elapsed time is 0.374925 seconds.
> > Elapsed time is 0.372524 seconds.

WOW - MAATT!...

% US's results
     zzus=0.139465; % <- ZEROS()
     zdus=0.000054; % <- ZEROS(...,'double')
% MF's results on his/her cpu...
     zzmf=0.374925;
% get projected ZEROS(...,'double') based on MF's cpu
     zdmf=zzmf/zzus*zdus;
     disp(zdmf);
% 0.00014517
% time wasted per call if ZEROS are NOT used properly...
     tsmf=zzmf-zdmf;
     disp(tsmf);
% 0.37478
% assume MF properly allocates 1'000'000 5000x5000 arrays/day
% (probably very conservative assumption given MF's typical ML chores)
% time wasted [sec]
     ts=1000000*tsmf;
% total time wasted by MF for ONE day of work...
     tw={
          ts 'secs'
          ts/60 'mins'
          ts/60/24 'days'
     };
     disp(tw);
%{
     [3.7478e+005] 'secs'
     [ 6246.3] 'mins'
     [ 260.26] 'days' % <- saving [d] IF(F) you had a newer ML version...
%}

given your old ML version, you're wasting almost ONE year of your live/your typical ML work-day for simple pre-allocation(!)...
and i don't even want to start to think of other bottlenecks...

do you want me to talk to your spouse re upgrading...

:-)
us

Subject: Preallocation in MATLAB R2009a

From: Yair Altman

Date: 5 Aug, 2009 21:14:02

Message: 25 of 31

Nathan <ngreco32@gmail.com> wrote ...
> On Aug 5, 12:36?pm, "Matt Fig" <spama...@yahoo.com> wrote:
> > "Yair Altman" <altmany...@gmailDEL.comDEL> wrote ...
> > > BTW, this is also the case in older Matlab versions, not just 2009a or 2008b.
> >
> > I see virtually no difference in 2007a.
[snip]
> Looks like Yair wasn't quite correct?
> -Nathan


I humbly stand corrected - my testing was flawed - didn't clear everything I should have between tests - careless...

Sorry for misleading

Yair

Subject: Preallocation in MATLAB R2009a

From: Matt Fig

Date: 5 Aug, 2009 21:18:05

Message: 26 of 31

"us " <us@neurol.unizh.ch> wrote in message <h5crqp$elm$1@fred.mathworks.com>...
> WOW - MAATT!...
>
> % US's results
> zzus=0.139465; % <- ZEROS()
> zdus=0.000054; % <- ZEROS(...,'double')
> % MF's results on his/her cpu...




His/Her? That is my real picture on the NG profile!





> zzmf=0.374925;
> % get projected ZEROS(...,'double') based on MF's cpu
> zdmf=zzmf/zzus*zdus;
> disp(zdmf);
> % 0.00014517
> % time wasted per call if ZEROS are NOT used properly...
> tsmf=zzmf-zdmf;
> disp(tsmf);
> % 0.37478
> % assume MF properly allocates 1'000'000 5000x5000 arrays/day
> % (probably very conservative assumption given MF's typical ML chores)




I think my fingers would fall off! (And I wouldn't have a spouse very long if I were creating a million matrices, of any size, per day every day!!)





> % time wasted [sec]
> ts=1000000*tsmf;
> % total time wasted by MF for ONE day of work...
> tw={
> ts 'secs'
> ts/60 'mins'
> ts/60/24 'days'
> };
> disp(tw);
> %{
> [3.7478e+005] 'secs'
> [ 6246.3] 'mins'
> [ 260.26] 'days' % <- saving [d] IF(F) you had a newer ML version...
> %}
>
> given your old ML version, you're wasting almost ONE year of your live/your typical ML work-day for simple pre-allocation(!)...
> and i don't even want to start to think of other bottlenecks...
>
> do you want me to talk to your spouse re upgrading...
>
> :-)
> us



I will hold out upgrading beyond 2007b for as long as possible because I really(!) don't like TMW new licensing model. I (personally) think it is a rip-off to their loyal customers (and unpaid ML evangelists/salesman such as myself).

But thanks for the thought! ;-)

Subject: Preallocation in MATLAB R2009a

From: James Tursa

Date: 6 Aug, 2009 00:20:18

Message: 27 of 31

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h5cror$ar2$1@fred.mathworks.com>...
> "James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <h5cq83$7o$1@fred.mathworks.com>...
> > Just a guess. Seems like the zeros function is not taking advantage
> > of that, where the a(etc,etc)=0 method does.
>
> But James, this does not explain the difference of speed when zeros() is invokes with and without specified classes as us has showed.

The only thing I can offer is what you already suggested ... that in one case something like memset is being called and in the other case it isn't. e.g., consider the following mex routine (zerosx.c):

#include <string.h>
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    double *dp;
    mwSize k, kk, v;
    k = mxGetScalar(prhs[0]);
    kk = k * k;
    dp = mxMalloc(kk*sizeof(*dp));
    plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL);
    mxSetM(plhs[0],k);
    mxSetN(plhs[0],k);
    mxSetPr(plhs[0],dp);
    if( nrhs == 2 ) {
        v = mxGetScalar(prhs[1]);
        memset( dp, v, 8*kk );
    }
}

This mex routine takes 1 or 2 inputs. The first input is the size of the array to create (input is k, output will be a k x k matrix), and the 2nd optional input is a value to fill the array with ... in this case I will be using 0. The key point is that memset is only called if you input the 2nd argument. So here are some timing tests on WinXp R2009a:

>> tic;a=zeros(4000);toc
Elapsed time is 0.081633 seconds.
>> clear a
>> tic;a=zeros(4000,'double');toc
Elapsed time is 0.000045 seconds.
>> clear a
>> tic;zerosx(4000,0);toc
Elapsed time is 0.081687 seconds.
>> clear a
>> tic;zerosx(4000);toc
Elapsed time is 0.000037 seconds.

So just based on a timing analysis I would conclude that your supposition is correct. Calling zeros *with* a class argument bypasses the memset equivalent function, probably with some internal assumption that the memory has already been zeroed out previously. But calling zeros *without* a class argument for some reason results in the memory being physically being set to zero. I don't know why the two should be different ... probably a programming oversight as you suggested.

James Tursa

Subject: Preallocation in MATLAB R2009a

From: rocwoods

Date: 6 Aug, 2009 14:44:02

Message: 28 of 31

Thanks to all the people who are concerned about this !

Subject: Preallocation in MATLAB R2009a

From: Gary

Date: 6 Aug, 2009 19:52:02

Message: 29 of 31

clear all
n = 5000;
tic;
r(n,n) = 0;
toc
clear all;
n = 5000;
tic;
r = zeros(n);
toc
clear all;
n = 5000;
tic
r = zeros(n,'double');
toc

Elapsed time is 0.001621 seconds. %r(n,n)=0
Elapsed time is 0.189088 seconds. %zeros(n)
Elapsed time is 0.002219 seconds. %zeros(n,'double')

Lenovo T61, T7100 Core 2 Duo, 1.8 GHz, 3GB RAM, WinXP SP2, Matlab 2009a

Subject: Preallocation in MATLAB R2009a

From: Clemens

Date: 28 Apr, 2014 08:50:17

Message: 30 of 31

Still persistent in 2012a:

n = 5000;
tic;
r(n,n) = 0;
toc
clear all;
n = 5000;
tic;
r = zeros(n);
toc
clear all;
n = 5000;
tic
r = zeros(n,'double');
toc

Elapsed time is 0.001233 seconds.
Elapsed time is 0.078649 seconds.
Elapsed time is 0.000957 seconds.

64-bit.

Subject: Preallocation in MATLAB R2009a

From: Loren Shure

Date: 28 Apr, 2014 17:09:06

Message: 31 of 31


"Clemens " <gu95lub@matlab.rbg.tum.de> wrote in message
news:ljl4o9$sba$1@newscl01ah.mathworks.com...
> Still persistent in 2012a:
>
> n = 5000;
> tic;
> r(n,n) = 0;
> toc
> clear all;
> n = 5000;
> tic;
> r = zeros(n);
> toc
> clear all;
> n = 5000;
> tic
> r = zeros(n,'double');
> toc
>
> Elapsed time is 0.001233 seconds.
> Elapsed time is 0.078649 seconds.
> Elapsed time is 0.000957 seconds.
>
> 64-bit.
>
Hi Clemens,

You are not quite evaluating apples to apples. First, timing is generally
best done in functions. We can optimize some parts of scripts, but since
they depend on current MATLAB state more than a function, they are not
always fully optimized. That said, I asked someone here about the
difference in timings. Here's what he said:

The short answer is that in the first case, the memory isn't truly allocated
until some operation is done.

I would also caution you not to rely on the results of a single call to make
generalizations about the behavior of a function or syntax.
While someone might look at this and think that instead of calling:

x = zeros(n); %case 1

it would be better to call

x(n,n) = 0;%case2

This would be a mistake.

The OS dictates when and how the memory gets allocated. So in case 1, we
have implemented zeros in such a way that forces the OS to "really" allocate
before the function returns. The subscripting version in case 2 may not be
doing this, so the OS waits and the call returns quickly.

The real optimization is that since the memory isn't being used, it never
"really" gets allocated in case 1.

However, operations which require that array may take longer. If we modify
the reproduction steps, we see the results are much different. Also, we see
different operations taking different amounts of time. The overall
performance of the combination of calls is about the same for each of these.

clear all
n = 5000;
Total = tic;
tic,x(n,n) = 0; toc
tic,x(floor(n/2)) = 1; toc
tic,y = sum(x(:)); toc
toc(Total)
disp(y)

clear all
n = 5000;
Total = tic;
tic,x=zeros(n); toc
tic,x(floor(n/2)) = 1; toc
tic,y = sum(x(:)); toc
toc(Total)
disp(y)

clear all
n = 5000;
Total = tic;
tic,x=zeros(n,'double'); toc
tic,x(floor(n/2)) = 1; toc
tic,y = sum(x(:)); toc
toc(Total)
disp(y)


Hope this is helpful.
--Loren

http://blogs.mathworks.com/loren

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us