Thread Subject: class property cell array indexed assignment is slow

Subject: class property cell array indexed assignment is slow

From: Michael Maurer

Date: 19 Dec, 2011 01:51:52

Message: 1 of 3

Using R2011b, I am assigning individual elements in a cell array,
where the cell array is a property in a handle class. I have found
that the assignment is very slow if the cell array is large. The
elapsed time is linear in the size of the cell array, even though I am
assigning only one element. It is as if the entire cell array is
copied on each assignment. The same code for numeric arrays runs in
constant time.

Here is sample code:

====================== sample.m
classdef sample < handle
  properties
    data
  end
  methods
    function self = sample(init)
      self.data = init;
    end
    function set(self, i, val)
      self.data(i) = val;
    end
  end % methods
end % classdef

====================== testsample.m
L1 = sample([]); % numeric test case
L2 = sample({}); % cell test case

for k = 10.^(0:6)
  % numeric case
  L1.set(k, 1);
  tic
  for i=1:10000
    L1.set(k, 1);
  end
  elap1 = toc;

  % cell case
  L2.set(k, {1});
  tic
  for i=1:10000
    L2.set(k, {1});
  end
  elap2 = toc;

  fprintf('%7d elements, elapsed numeric %.1f, elapsed cell %.1f\n',
k, elap1, elap2);
end

======================
>> testsample
      1 elements, elapsed numeric 0.2, elapsed cell 0.3
     10 elements, elapsed numeric 0.2, elapsed cell 0.3
    100 elements, elapsed numeric 0.2, elapsed cell 0.3
   1000 elements, elapsed numeric 0.2, elapsed cell 0.5
  10000 elements, elapsed numeric 0.2, elapsed cell 2.2
 100000 elements, elapsed numeric 0.2, elapsed cell 19.5
1000000 elements, elapsed numeric 0.2, elapsed cell 248.6
======================

I have found a few old threads that may be referring to a similar
issue:

"slow class property access problem and solution" 8/6/2010
http://www.mathworks.com/matlabcentral/newsreader/view_thread/288746

"Cell Array 1e2 Slower When Object Property?" 1/6/2009
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241982


Am I making some kind of blunder?

Thanks,
Michael M

Subject: class property cell array indexed assignment is slow

From: Xianfeng

Date: 12 Jan, 2012 19:17:08

Message: 2 of 3

I am facing to the same porblem, the reason is the entire cell array is copied on each assignment.

Michael Maurer <mjmaurer@gmail.com> wrote in message <ff20f790-0ab6-440e-a2f7-790041695eda@z12g2000yqm.googlegroups.com>...
> Using R2011b, I am assigning individual elements in a cell array,
> where the cell array is a property in a handle class. I have found
> that the assignment is very slow if the cell array is large. The
> elapsed time is linear in the size of the cell array, even though I am
> assigning only one element. It is as if the entire cell array is
> copied on each assignment. The same code for numeric arrays runs in
> constant time.
>
> Here is sample code:
>
> ====================== sample.m
> classdef sample < handle
> properties
> data
> end
> methods
> function self = sample(init)
> self.data = init;
> end
> function set(self, i, val)
> self.data(i) = val;
> end
> end % methods
> end % classdef
>
> ====================== testsample.m
> L1 = sample([]); % numeric test case
> L2 = sample({}); % cell test case
>
> for k = 10.^(0:6)
> % numeric case
> L1.set(k, 1);
> tic
> for i=1:10000
> L1.set(k, 1);
> end
> elap1 = toc;
>
> % cell case
> L2.set(k, {1});
> tic
> for i=1:10000
> L2.set(k, {1});
> end
> elap2 = toc;
>
> fprintf('%7d elements, elapsed numeric %.1f, elapsed cell %.1f\n',
> k, elap1, elap2);
> end
>
> ======================
> >> testsample
> 1 elements, elapsed numeric 0.2, elapsed cell 0.3
> 10 elements, elapsed numeric 0.2, elapsed cell 0.3
> 100 elements, elapsed numeric 0.2, elapsed cell 0.3
> 1000 elements, elapsed numeric 0.2, elapsed cell 0.5
> 10000 elements, elapsed numeric 0.2, elapsed cell 2.2
> 100000 elements, elapsed numeric 0.2, elapsed cell 19.5
> 1000000 elements, elapsed numeric 0.2, elapsed cell 248.6
> ======================
>
> I have found a few old threads that may be referring to a similar
> issue:
>
> "slow class property access problem and solution" 8/6/2010
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/288746
>
> "Cell Array 1e2 Slower When Object Property?" 1/6/2009
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/241982
>
>
> Am I making some kind of blunder?
>
> Thanks,
> Michael M

Subject: class property cell array indexed assignment is slow

From: Titus Edelhofer

Date: 24 Feb, 2012 14:05:40

Message: 3 of 3

It's the way of indexing that workes but makes it slow: your method for cells does index like x(i) = {value}; instead of x{i} = value;
The second is much faster. So if you add a method setCell like follows:
function setCell(self, i, val)
  self.data{i} = val;
end

and call in testsample.m

L2.setCell(k, 1);

you get the same results...

Titus

"Xianfeng " <vbx7@cdc.gov> wrote in message <jenbjk$jf9$1@newscl01ah.mathworks.com>...
> I am facing to the same porblem, the reason is the entire cell array is copied on each assignment.
>
> Michael Maurer <mjmaurer@gmail.com> wrote in message <ff20f790-0ab6-440e-a2f7-790041695eda@z12g2000yqm.googlegroups.com>...
> > Using R2011b, I am assigning individual elements in a cell array,
> > where the cell array is a property in a handle class. I have found
> > that the assignment is very slow if the cell array is large. The
> > elapsed time is linear in the size of the cell array, even though I am
> > assigning only one element. It is as if the entire cell array is
> > copied on each assignment. The same code for numeric arrays runs in
> > constant time.
> >
> > Here is sample code:
> >
> > ====================== sample.m
> > classdef sample < handle
> > properties
> > data
> > end
> > methods
> > function self = sample(init)
> > self.data = init;
> > end
> > function set(self, i, val)
> > self.data(i) = val;
> > end
> > end % methods
> > end % classdef
> >
> > ====================== testsample.m
> > L1 = sample([]); % numeric test case
> > L2 = sample({}); % cell test case
> >
> > for k = 10.^(0:6)
> > % numeric case
> > L1.set(k, 1);
> > tic
> > for i=1:10000
> > L1.set(k, 1);
> > end
> > elap1 = toc;
> >
> > % cell case
> > L2.set(k, {1});
> > tic
> > for i=1:10000
> > L2.set(k, {1});
> > end
> > elap2 = toc;
> >
> > fprintf('%7d elements, elapsed numeric %.1f, elapsed cell %.1f\n',
> > k, elap1, elap2);
> > end
> >
> > ======================
> > >> testsample
> > 1 elements, elapsed numeric 0.2, elapsed cell 0.3
> > 10 elements, elapsed numeric 0.2, elapsed cell 0.3
> > 100 elements, elapsed numeric 0.2, elapsed cell 0.3
> > 1000 elements, elapsed numeric 0.2, elapsed cell 0.5
> > 10000 elements, elapsed numeric 0.2, elapsed cell 2.2
> > 100000 elements, elapsed numeric 0.2, elapsed cell 19.5
> > 1000000 elements, elapsed numeric 0.2, elapsed cell 248.6
> > ======================
> >
> > I have found a few old threads that may be referring to a similar
> > issue:
> >
> > "slow class property access problem and solution" 8/6/2010
> > http://www.mathworks.com/matlabcentral/newsreader/view_thread/288746
> >
> > "Cell Array 1e2 Slower When Object Property?" 1/6/2009
> > http://www.mathworks.com/matlabcentral/newsreader/view_thread/241982
> >
> >
> > Am I making some kind of blunder?
> >
> > Thanks,
> > Michael M

Tags for this Thread

Everyone's Tags:

oop

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
oop per isakson 20 Dec, 2011 14:05:11
rssFeed for this Thread

Contact us at files@mathworks.com