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:
Assigning value to a class property extremely slow

Subject: Assigning value to a class property extremely slow

From: Ralph Kutschera

Date: 21 Dec, 2009 14:22:22

Message: 1 of 14

Hallo!

   I'm developing an object-oriented application with MATLAB R2008a and
wonder how it is possible that assigning _one_ double value to a class
property can take 0.04 seconds.

This is the basic class concept:
Base class BASE holds _one_ FEM object. The FEM object itself holds
usually some hundred FE objects.

classdef BASE
   properties
     FEM;
   end

   methods
     function this = BASE()
       this.FEM = FEMpack.FEM(); % another object
     end

     function this = whatever()
       anArray = [1 5 8 10 200];
       for i = 1:length(anArray)
         this.FEM.FE{anArray(i)}.Alpha = 1;
       end
     end

     ...
   end
end

classdef FEM
   properties
     FE;
   end

   methods
     function FEM()
       this.FE = cell(1,1000);
       for i = 1:1000
         this.FE{i} = FEMpack.FE();
       end
     end

     ...
   end
end

Now when running, the for-loop consumes 34 seconds for assigning 630
values. How can this be possible?

time calls
  0.01 630 for i = 1:length(anArray)
34.34 630 this.FEM{FE(anArray(i))}.Alpha = 1;
  0.01 630 end

Thank you,
   Ralph

Subject: Assigning value to a class property extremely slow

From: Ralph Kutschera

Date: 21 Dec, 2009 14:30:51

Message: 2 of 14

Ralph Kutschera schrieb:
> This is the basic class concept:
> Base class BASE holds _one_ FEM object. The FEM object itself holds
> usually some hundred FE objects.

Maybe of interest:

BASE < handle
FEM < handle
FE < handle

BASE holds _one_ FEM
FEM holds several FE

Subject: Assigning value to a class property extremely slow

From: Matt J

Date: 21 Dec, 2009 15:14:21

Message: 3 of 14

Ralph Kutschera <news2010@ecuapac.at> wrote in message <4b2f848f$0$12642$3b214f66@aconews.univie.ac.at>...

> for i = 1:length(anArray)
> this.FEM.FE{anArray(i)}.Alpha = 1;
> end

This

this.FEM.FE{anArray(i)}.Alpha

is a very long and very deeply nested indexing expression. I would imagine this is what is taking so much time.

One thing you might want to be aware of is that MATLAB needs to evaluate each layer of the index nest in order to determine what set/get methods it needs to call and so forth. In other words, when MATLAB sees the above expression, it's expanding it into the following steps

A=this
B=A.FEM
C=B.FE
D=C{anArray(i)}
E=D.Alpha

Only now does MATLAB know what kind of object Alpha actually is (and whereit is) and can perform the assignment

E=1

Subject: Assigning value to a class property extremely slow

From: Ralph Kutschera

Date: 21 Dec, 2009 15:35:38

Message: 4 of 14

Matt J schrieb:
> this.FEM.FE{anArray(i)}.Alpha
> is a very long and very deeply nested indexing expression. I would
> imagine this is what is taking so much time.
> One thing you might want to be aware of is that MATLAB needs to evaluate
> each layer of the index nest in order to determine what set/get methods
> it needs to call and so forth. In other words, when MATLAB sees the
> above expression, it's expanding it into the following steps
>
> A=this
> B=A.FEM
> C=B.FE
> D=C{anArray(i)}
> E=D.Alpha
>
> Only now does MATLAB know what kind of object Alpha actually is (and
> whereit is) and can perform the assignment
>
> E=1

Hi and thanks,

   I think it's actually the assignment operation itself. I tested the
following:

% assignment
this.FEM.FE{anArray(i)}.Alpha = 1; > takes ~ 30 seconds

% just do nothing
this.FEM.FE{anArray(i)}.Alpha; > takes ~ 0.25 seconds

% displaying Alpha
this.FEM.FE{anArray(i)}.Alpha > takes ~ 5 seconds

Ralph

Subject: Assigning value to a class property extremely slow

From: Matt J

Date: 21 Dec, 2009 15:48:04

Message: 5 of 14

Ralph Kutschera <news2010@ecuapac.at> wrote in message <4b2f95ba$0$12642$3b214f66@aconews.univie.ac.at>...
> Matt J schrieb:
> > this.FEM.FE{anArray(i)}.Alpha
> > is a very long and very deeply nested indexing expression. I would
> > imagine this is what is taking so much time.
> > One thing you might want to be aware of is that MATLAB needs to evaluate
> > each layer of the index nest in order to determine what set/get methods
> > it needs to call and so forth. In other words, when MATLAB sees the
> > above expression, it's expanding it into the following steps
> >
> > A=this
> > B=A.FEM
> > C=B.FE
> > D=C{anArray(i)}
> > E=D.Alpha
> >
> > Only now does MATLAB know what kind of object Alpha actually is (and
> > whereit is) and can perform the assignment
> >
> > E=1
>
> Hi and thanks,
>
> I think it's actually the assignment operation itself. I tested the
> following:
>
> % assignment
> this.FEM.FE{anArray(i)}.Alpha = 1; > takes ~ 30 seconds

Is Alpha an empty structure field before this assignment takes place? If so, MATLAB must do the necessary memory allocation at the time it is assigned something non-empty. You should preallocate this.FEM.FE{anArray(i)}.Alpha to contain a scalar.

What happens when you rerun the loop, overwriting the 1 with another 1? Is it no faster?

Subject: Assigning value to a class property extremely slow

From: Ralph Kutschera

Date: 21 Dec, 2009 16:03:51

Message: 6 of 14

Matt J schrieb:
>> Hi and thanks,
>>
>> I think it's actually the assignment operation itself. I tested the
>> following:
>>
>> % assignment
>> this.FEM.FE{anArray(i)}.Alpha = 1; > takes ~ 30 seconds
>
> Is Alpha an empty structure field before this assignment takes place? If
> so, MATLAB must do the necessary memory allocation at the time it is
> assigned something non-empty. You should preallocate
> this.FEM.FE{anArray(i)}.Alpha to contain a scalar.

Alpha gets initialized withing the constructor and is defaulted to 1.0;


> What happens when you rerun the loop, overwriting the 1 with another 1?
> Is it no faster?

Doing the assignment twice doubles the time consumption:

this.FEM.FE{anArray(i)}.Alpha = 1;
this.FEM.FE{anArray(i)}.Alpha = 1;
 > ~ 60 seconds

Ralph

Subject: Assigning value to a class property extremely slow

From: Matt J

Date: 21 Dec, 2009 16:21:20

Message: 7 of 14

Ralph Kutschera <news2010@ecuapac.at> wrote in message <4b2f9c58$0$11868$3b214f66@aconews.univie.ac.at>...
> Matt J schrieb:
> >> Hi and thanks,
> >>
> >> I think it's actually the assignment operation itself. I tested the
> >> following:
> >>
> >> % assignment
> >> this.FEM.FE{anArray(i)}.Alpha = 1; > takes ~ 30 seconds
> >
> > Is Alpha an empty structure field before this assignment takes place? If
> > so, MATLAB must do the necessary memory allocation at the time it is
> > assigned something non-empty. You should preallocate
> > this.FEM.FE{anArray(i)}.Alpha to contain a scalar.
>
> Alpha gets initialized withing the constructor and is defaulted to 1.0;
>
>
> > What happens when you rerun the loop, overwriting the 1 with another 1?
> > Is it no faster?
>
> Doing the assignment twice doubles the time consumption:
>
> this.FEM.FE{anArray(i)}.Alpha = 1;
> this.FEM.FE{anArray(i)}.Alpha = 1;
> > ~ 60 seconds
>

Dunno. Have you defined any property set/get methods for FE or FEM that you haven't shown us. Or perhaps a subsasgn/subsref method?

 

Subject: Assigning value to a class property extremely slow

From: Philip Borghesani

Date: 21 Dec, 2009 17:06:46

Message: 8 of 14


"Ralph Kutschera" <news2010@ecuapac.at> wrote in message news:4b2f848f$0$12642$3b214f66@aconews.univie.ac.at...
> Hallo!
>
> I'm developing an object-oriented application with MATLAB R2008a and wonder how it is possible that assigning _one_ double value
> to a class property can take 0.04 seconds.
>
> This is the basic class concept:
> Base class BASE holds _one_ FEM object. The FEM object itself holds usually some hundred FE objects.
>
> classdef BASE
> properties
> FEM;
> end
>
> methods
> function this = BASE()
> this.FEM = FEMpack.FEM(); % another object
> end
>
> function this = whatever()
> anArray = [1 5 8 10 200];
> for i = 1:length(anArray)
> this.FEM.FE{anArray(i)}.Alpha = 1;
> end
> end
>
> ...
> end
> end
>
> classdef FEM
> properties
> FE;
> end
>
> methods
> function FEM()
> this.FE = cell(1,1000);
> for i = 1:1000
> this.FE{i} = FEMpack.FE();
> end
> end
>
> ...
> end
> end
>
> Now when running, the for-loop consumes 34 seconds for assigning 630 values. How can this be possible?
>
> time calls
> 0.01 630 for i = 1:length(anArray)
> 34.34 630 this.FEM{FE(anArray(i))}.Alpha = 1;
> 0.01 630 end
>
> Thank you,
> Ralph
>

Try taking some of the assignment out of the loop. This code will not run because the example you posted will not run but this
shows the idea.

     function this = whatever()
       anArray = [1 5 8 10 200];
       FE=this.FEM.FE;
       for i = 1:length(anArray)
         FE{anArray(i)}.Alpha = 1;
       end
       this.FEM.FE=FE;
     end

You did not specify what platform and version of MATLAB you are using but some improvement has been made in the performance of
assigning to properties containing large structures of handle objects in recent versions of MATLAB.

Phil

Subject: Assigning value to a class property extremely slow

From: Ralph Kutschera

Date: 22 Dec, 2009 19:29:40

Message: 9 of 14

Philip Borghesani schrieb:
> Try taking some of the assignment out of the loop. This code will not run because the example you posted will not run but this
> shows the idea.
>
> function this = whatever()
> anArray = [1 5 8 10 200];
> FE=this.FEM.FE;
> for i = 1:length(anArray)
> FE{anArray(i)}.Alpha = 1;
> end
> this.FEM.FE=FE;
> end

Thanks you very very much! This idea is worth factor 500 or more :)


> You did not specify what platform and version of MATLAB you are using but some improvement has been made in the performance of
> assigning to properties containing large structures of handle objects in recent versions of MATLAB.

I'm using R2008a. Will try it out if I'm able to get 2009 or so!

Merry christmas!

Ralph

Subject: Assigning value to a class property extremely slow

From: LP Pakula

Date: 18 Feb, 2010 06:19:05

Message: 10 of 14

Hi Ralph & Readers

I too have found property setting/constructing/etc **extremely** slow.
In one case, i need to construct n x n identical objects to an array of objects, ie

% This is Slow
myObject = ClassX(); % define type
myObject(n,n) = ClassX(); % expand array
copyObject = ClassX(prams);
for i=1:n
   for j=1:n
      myObject(i,j) = copyObject;
    end
end

% This is FASTER
myObject = ClassX(); % define type
myObject(n,n) = ClassX(); % expand array
myObject(:,:) = ClassX(prams); % set all the same

Now of course, the main problem is what if you have params that differ for each element? And often, these objects will act on different data so you simply must use a for loop and then the speed drop is even more dramatic because you can't simply use a "copy".

% e.g. VERY VERY SLOW
for i=1:n
   for j=1:n
      myObject(i,j) = myObject(i,j).someMethod(uniqueData(i,j))
    end
end

* I've noted using Handle classes improves the speed about two-fold.
* More aggregation of classes leads to more performance drops (ie myObject.myNestedObject.etcObject)

I'm using R2008b.

Cheers, Lyle

Subject: Assigning value to a class property extremely slow

From: Matt J

Date: 18 Feb, 2010 09:14:23

Message: 11 of 14

"LP Pakula" <lylexxxxxxx@gmail.com> wrote in message <hlim4p$h4$1@fred.mathworks.com>...

> Now of course, the main problem is what if you have params that differ for each element? And often, these objects will act on different data so you simply must use a for loop and then the speed drop is even more dramatic because you can't simply use a "copy".
>
> % e.g. VERY VERY SLOW
> for i=1:n
> for j=1:n
> myObject(i,j) = myObject(i,j).someMethod(uniqueData(i,j))
> end
> end
>
==================


See p. 8-22 in the OOP manual. You should be passing the entire array uniqueData drectly to the constructor

myObject=ClassX(uniqueData); % make this create the nxn object array


Whether you need to use a double for-loop inside the class constructor for ClassX is unclear, since we don't know what someMethod() does. As usual, it is better to try to vectorize assignments in MATLAB and to avoid for-loops.

Subject: Assigning value to a class property extremely slow

From: LP Pakula

Date: 21 Feb, 2010 21:13:03

Message: 12 of 14

I believe the following workaround might be useful for other people in this position. I had noted that calling methods was *much* faster than calling the constructor so, i now wrap the constructor in a method called "constructor" and just put the

e.g.

classdef someClass



"LP Pakula" <lylexxxxxxx@gmail.com> wrote in message <hlim4p$h4$1@fred.mathworks.com>...
> Hi Ralph & Readers
>
> I too have found property setting/constructing/etc **extremely** slow.
> In one case, i need to construct n x n identical objects to an array of objects, ie
>
> % This is Slow
> myObject = ClassX(); % define type
> myObject(n,n) = ClassX(); % expand array
> copyObject = ClassX(prams);
> for i=1:n
> for j=1:n
> myObject(i,j) = copyObject;
> end
> end
>
> % This is FASTER
> myObject = ClassX(); % define type
> myObject(n,n) = ClassX(); % expand array
> myObject(:,:) = ClassX(prams); % set all the same
>
> Now of course, the main problem is what if you have params that differ for each element? And often, these objects will act on different data so you simply must use a for loop and then the speed drop is even more dramatic because you can't simply use a "copy".
>
> % e.g. VERY VERY SLOW
> for i=1:n
> for j=1:n
> myObject(i,j) = myObject(i,j).someMethod(uniqueData(i,j))
> end
> end
>
> * I've noted using Handle classes improves the speed about two-fold.
> * More aggregation of classes leads to more performance drops (ie myObject.myNestedObject.etcObject)
>
> I'm using R2008b.
>
> Cheers, Lyle

Subject: Assigning value to a class property extremely slow

From: LP Pakula

Date: 21 Feb, 2010 21:13:03

Message: 13 of 14

I believe the following workaround might be useful for other people in this position. I had noted that calling methods was *much* faster than calling the constructor so, i now wrap the constructor in a method called "constructor" and just put the

e.g.

classdef someClass



"LP Pakula" <lylexxxxxxx@gmail.com> wrote in message <hlim4p$h4$1@fred.mathworks.com>...
> Hi Ralph & Readers
>
> I too have found property setting/constructing/etc **extremely** slow.
> In one case, i need to construct n x n identical objects to an array of objects, ie
>
> % This is Slow
> myObject = ClassX(); % define type
> myObject(n,n) = ClassX(); % expand array
> copyObject = ClassX(prams);
> for i=1:n
> for j=1:n
> myObject(i,j) = copyObject;
> end
> end
>
> % This is FASTER
> myObject = ClassX(); % define type
> myObject(n,n) = ClassX(); % expand array
> myObject(:,:) = ClassX(prams); % set all the same
>
> Now of course, the main problem is what if you have params that differ for each element? And often, these objects will act on different data so you simply must use a for loop and then the speed drop is even more dramatic because you can't simply use a "copy".
>
> % e.g. VERY VERY SLOW
> for i=1:n
> for j=1:n
> myObject(i,j) = myObject(i,j).someMethod(uniqueData(i,j))
> end
> end
>
> * I've noted using Handle classes improves the speed about two-fold.
> * More aggregation of classes leads to more performance drops (ie myObject.myNestedObject.etcObject)
>
> I'm using R2008b.
>
> Cheers, Lyle

Subject: Assigning value to a class property extremely slow

From: LP Pakula

Date: 21 Feb, 2010 21:37:02

Message: 14 of 14

I believe i may have a workaround for others having issues with slow OOP for large arrays of non-vectorizable problems;

1) Make your Class a handle class
2) Methods appear to access much faster than constructors so wrap all constructors in a method, perhaps called "constructor"

e.g.
classdef someClass
methods
  function obj = someClass(varargin)
    if nargin > 0
          obj.construct(varargin{:})
     end
   end
 
  function construct(arg1, arg2, arg3)
     obj.someProperty = arg1;
     etc
  end
end
end

2) When constructing large arrays of objects
% This is FASTER
myObject = someClass(); % define type
myObject(n,n) = someClass(); % expand array
for i=1:n
    for j=1:n
       myObject(i,j).construct(arg1,arg2,arg3);
     end
end

It appears that calling the empty constructor to allocate all the memory (etc) is very vast. Then going over the array and actually populating the properties is fast.

Cheers, Lyle

"LP Pakula" <lylexxxxxxx@gmail.com> wrote in message <hlim4p$h4$1@fred.mathworks.com>...
> Hi Ralph & Readers
>
> I too have found property setting/constructing/etc **extremely** slow.
> In one case, i need to construct n x n identical objects to an array of objects, ie
>
> % This is Slow
> myObject = ClassX(); % define type
> myObject(n,n) = ClassX(); % expand array
> copyObject = ClassX(prams);
> for i=1:n
> for j=1:n
> myObject(i,j) = copyObject;
> end
> end
>
> % This is FASTER
> myObject = ClassX(); % define type
> myObject(n,n) = ClassX(); % expand array
> myObject(:,:) = ClassX(prams); % set all the same
>
> Now of course, the main problem is what if you have params that differ for each element? And often, these objects will act on different data so you simply must use a for loop and then the speed drop is even more dramatic because you can't simply use a "copy".
>
> % e.g. VERY VERY SLOW
> for i=1:n
> for j=1:n
> myObject(i,j) = myObject(i,j).someMethod(uniqueData(i,j))
> end
> end
>
> * I've noted using Handle classes improves the speed about two-fold.
> * More aggregation of classes leads to more performance drops (ie myObject.myNestedObject.etcObject)
>
> I'm using R2008b.
>
> Cheers, Lyle

Tags for this Thread

No tags are associated with 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