Thread Subject: Why are cell arrays so much faster?

Subject: Why are cell arrays so much faster?

From: David Doria

Date: 11 Jul, 2008 20:00:23

Message: 1 of 8

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?

Thanks,

Dave

Subject: Why are cell arrays so much faster?

From: Kenneth Eaton

Date: 11 Jul, 2008 20:32:05

Message: 2 of 8

"David Doria" <daviddoria@gmail.com> wrote in message
<g58e4n$13t$1@fred.mathworks.com>...
> 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?
>
> Thanks,
>
> Dave

I couldn't reproduce those results, so I couldn't tell you
what's going on. I did, however, make an interesting
discovery:

>> tic; a=repmat(1000,300,360); toc;
Elapsed time is 0.003951 seconds.
>> tic; b=1000.*ones(300,360); toc;
Elapsed time is 0.005800 seconds.

For some reason, I always thought of repmat as being slower
than matrix operations. Maybe I'll start using it more now
=).

Ken

Subject: Why are cell arrays so much faster?

From: Peter Boettcher

Date: 11 Jul, 2008 20:37:52

Message: 3 of 8

"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

Subject: Why are cell arrays so much faster?

From: NZTideMan

Date: 11 Jul, 2008 21:03:32

Message: 4 of 8

On Jul 12, 8:37=A0am, Peter Boettcher <boettc...@ll.mit.edu> wrote:
> "David Doria" <daviddo...@gmail.com> writes:
> > Below is my experiment - I actually expected the opposite!
>
> >>> tic; CubeA(1:300, 1:360, 1) =3D repmat(1000,300,360); toc
> > Elapsed time is 0.003197 seconds.
> >>> tic; CubeB{1} =3D repmat(1000,300,360); toc
> > Elapsed time is 0.000964 seconds.
>
> > Why is this the case? =A0
>
> A cell array element is for all practical purposes the same as a
> separate variable. =A0So really this is just the difference between
>
> CubeA(1:300, 1:360, 1) =3D repmat(1000,300,360);
>
> and
>
> CubeB =3D repmat(1000,300,360);
>
> The first includes array indices on the left side, so it includes
> subarray assignment into the CubeA variable. =A0The 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. =A0MATLAB 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. =A0In the second case, no suc=
h
> thing would ever happen.
>
> -Peter

But how accurate is tic/toc?
You're measuring the difference between 3 milliseconds and 0.9
milliseconds.
A better test would be to put the commands within a large for/end
loop, say 10000 iterations:

>> tic;for it=3D1:10000,CubeA(1:300, 1:360, 1) =3D repmat(1000,300,360);end=
,toc

Elapsed time is 30.724523 seconds.

>> tic;for it=3D1:10000,CubeB{1} =3D repmat(1000,300,360);end,toc

Elapsed time is 19.932247 seconds.

Now I believe it.

Subject: Why are cell arrays so much faster?

From: Matt Fig

Date: 11 Jul, 2008 21:05:06

Message: 5 of 8


>
> >> tic; a=repmat(1000,300,360); toc;
> Elapsed time is 0.003951 seconds.
> >> tic; b=1000.*ones(300,360); toc;
> Elapsed time is 0.005800 seconds.
>
> For some reason, I always thought of repmat as being slower
> than matrix operations. Maybe I'll start using it more now
> =).
>
> Ken

On my machine:

tic; a=repmat(1000,300,360); toc;
Elapsed time is 0.035981 seconds.
tic; b=1000.*ones(300,360); toc;
Elapsed time is 0.003114 seconds.

Subject: Why are cell arrays so much faster?

From: Carlos Adrian Vargas Aguilera

Date: 11 Jul, 2008 21:11:01

Message: 6 of 8


> For some reason, I always thought of repmat as being slower
> than matrix operations. Maybe I'll start using it more now
> =).
>
> Ken

An once you use it, you'll see that the matrix way and
others like repeated index look so UGLY!

Carlos

Subject: Why are cell arrays so much faster?

From: Steve Amphlett

Date: 12 Jul, 2008 14:24:02

Message: 7 of 8

NZTideMan <mulgor@gmail.com> wrote in message <41094624-
3981-4166-907d-d4d493b87466@8g2000hse.googlegroups.com>...
> On Jul 12, 8:37=A0am, Peter Boettcher
<boettc...@ll.mit.edu> wrote:
> > "David Doria" <daviddo...@gmail.com> writes:
> > > Below is my experiment - I actually expected the
opposite!
> >
> > >>> tic; CubeA(1:300, 1:360, 1) =3D repmat
(1000,300,360); toc
> > > Elapsed time is 0.003197 seconds.
> > >>> tic; CubeB{1} =3D repmat(1000,300,360); toc
> > > Elapsed time is 0.000964 seconds.
> >
> > > Why is this the case? =A0
> >
> > A cell array element is for all practical purposes the
same as a
> > separate variable. =A0So really this is just the
difference between
> >
> > CubeA(1:300, 1:360, 1) =3D repmat(1000,300,360);
> >
> > and
> >
> > CubeB =3D repmat(1000,300,360);
> >
> > The first includes array indices on the left side, so
it includes
> > subarray assignment into the CubeA variable. =A0The
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. =A0MATLAB
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. =A0In the
second case, no suc=
> h
> > thing would ever happen.
> >
> > -Peter
>
> But how accurate is tic/toc?
> You're measuring the difference between 3 milliseconds
and 0.9
> milliseconds.
> A better test would be to put the commands within a large
for/end
> loop, say 10000 iterations:
>
> >> tic;for it=3D1:10000,CubeA(1:300, 1:360, 1) =3D repmat
(1000,300,360);end=
> ,toc
>
> Elapsed time is 30.724523 seconds.
>
> >> tic;for it=3D1:10000,CubeB{1} =3D repmat
(1000,300,360);end,toc
>
> Elapsed time is 19.932247 seconds.
>
> Now I believe it.

One piece is still missing though. The OP never said
whether his CubeA was pre-assigned.

Subject: Why are cell arrays so much faster?

From: David Doria

Date: 12 Jul, 2008 15:08:02

Message: 8 of 8

Nope, nothing was done before the lines of code I posted.

Dave

Tags for this Thread

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.

rssFeed for this Thread

Contact us at files@mathworks.com