<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172423</link>
    <title>MATLAB Central Newsreader - Why are cell arrays so much faster?</title>
    <description>Feed for thread: Why are cell arrays so much faster?</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Fri, 11 Jul 2008 20:00:23 -0400</pubDate>
      <title>Why are cell arrays so much faster?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172423#442570</link>
      <author>David Doria</author>
      <description>Below is my experiment - I actually expected the opposite!&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; tic; CubeA(1:300, 1:360, 1) = repmat(1000,300,360); toc&lt;br&gt;
Elapsed time is 0.003197 seconds.&lt;br&gt;
&amp;gt;&amp;gt; tic; CubeB{1} = repmat(1000,300,360); toc&lt;br&gt;
Elapsed time is 0.000964 seconds.&lt;br&gt;
&lt;br&gt;
Why is this the case?  &lt;br&gt;
&lt;br&gt;
Thanks,&lt;br&gt;
&lt;br&gt;
Dave</description>
    </item>
    <item>
      <pubDate>Fri, 11 Jul 2008 20:32:05 -0400</pubDate>
      <title>Re: Why are cell arrays so much faster?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172423#442578</link>
      <author>Kenneth Eaton</author>
      <description>&quot;David Doria&quot; &amp;lt;daviddoria@gmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;lt;g58e4n$13t$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Below is my experiment - I actually expected the opposite!&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; tic; CubeA(1:300, 1:360, 1) = repmat(1000,300,360); toc&lt;br&gt;
&amp;gt; Elapsed time is 0.003197 seconds.&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; tic; CubeB{1} = repmat(1000,300,360); toc&lt;br&gt;
&amp;gt; Elapsed time is 0.000964 seconds.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Why is this the case?  &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Dave&lt;br&gt;
&lt;br&gt;
I couldn't reproduce those results, so I couldn't tell you &lt;br&gt;
what's going on. I did, however, make an interesting &lt;br&gt;
discovery:&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; tic; a=repmat(1000,300,360); toc;&lt;br&gt;
Elapsed time is 0.003951 seconds.&lt;br&gt;
&amp;gt;&amp;gt; tic; b=1000.*ones(300,360); toc;&lt;br&gt;
Elapsed time is 0.005800 seconds.&lt;br&gt;
&lt;br&gt;
For some reason, I always thought of repmat as being slower &lt;br&gt;
than matrix operations. Maybe I'll start using it more now &lt;br&gt;
=).&lt;br&gt;
&lt;br&gt;
Ken</description>
    </item>
    <item>
      <pubDate>Fri, 11 Jul 2008 20:37:52 -0400</pubDate>
      <title>Re: Why are cell arrays so much faster?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172423#442579</link>
      <author>Peter Boettcher</author>
      <description>&quot;David Doria&quot; &amp;lt;daviddoria@gmail.com&amp;gt; writes:&lt;br&gt;
&lt;br&gt;
&amp;gt; Below is my experiment - I actually expected the opposite!&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt;&amp;gt; tic; CubeA(1:300, 1:360, 1) = repmat(1000,300,360); toc&lt;br&gt;
&amp;gt; Elapsed time is 0.003197 seconds.&lt;br&gt;
&amp;gt;&amp;gt;&amp;gt; tic; CubeB{1} = repmat(1000,300,360); toc&lt;br&gt;
&amp;gt; Elapsed time is 0.000964 seconds.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Why is this the case?  &lt;br&gt;
&lt;br&gt;
A cell array element is for all practical purposes the same as a&lt;br&gt;
separate variable.  So really this is just the difference between&lt;br&gt;
&lt;br&gt;
CubeA(1:300, 1:360, 1) = repmat(1000,300,360);&lt;br&gt;
&lt;br&gt;
and&lt;br&gt;
&lt;br&gt;
CubeB = repmat(1000,300,360);&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
The first includes array indices on the left side, so it includes&lt;br&gt;
subarray assignment into the CubeA variable.  The second simply names&lt;br&gt;
the output from repmat.&lt;br&gt;
&lt;br&gt;
I can't predict or guess at how the just-in-time compiler or the&lt;br&gt;
accelerator will react to the first line.  MATLAB might actually incur&lt;br&gt;
the cost of allocating two arrays of the same size, one for a new&lt;br&gt;
variable called CubeA, and the other for the temporary output of repmat,&lt;br&gt;
and a memcpy to do the subarray assignment.  In the second case, no such&lt;br&gt;
thing would ever happen.&lt;br&gt;
&lt;br&gt;
-Peter</description>
    </item>
    <item>
      <pubDate>Fri, 11 Jul 2008 21:03:32 -0400</pubDate>
      <title>Re: Why are cell arrays so much faster?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172423#442580</link>
      <author>NZTideMan</author>
      <description>On Jul 12, 8:37=A0am, Peter Boettcher &amp;lt;boettc...@ll.mit.edu&amp;gt; wrote:&lt;br&gt;
&amp;gt; &quot;David Doria&quot; &amp;lt;daviddo...@gmail.com&amp;gt; writes:&lt;br&gt;
&amp;gt; &amp;gt; Below is my experiment - I actually expected the opposite!&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt;&amp;gt; tic; CubeA(1:300, 1:360, 1) =3D repmat(1000,300,360); toc&lt;br&gt;
&amp;gt; &amp;gt; Elapsed time is 0.003197 seconds.&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt;&amp;gt; tic; CubeB{1} =3D repmat(1000,300,360); toc&lt;br&gt;
&amp;gt; &amp;gt; Elapsed time is 0.000964 seconds.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; Why is this the case? =A0&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; A cell array element is for all practical purposes the same as a&lt;br&gt;
&amp;gt; separate variable. =A0So really this is just the difference between&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; CubeA(1:300, 1:360, 1) =3D repmat(1000,300,360);&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; and&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; CubeB =3D repmat(1000,300,360);&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; The first includes array indices on the left side, so it includes&lt;br&gt;
&amp;gt; subarray assignment into the CubeA variable. =A0The second simply names&lt;br&gt;
&amp;gt; the output from repmat.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I can't predict or guess at how the just-in-time compiler or the&lt;br&gt;
&amp;gt; accelerator will react to the first line. =A0MATLAB might actually incur&lt;br&gt;
&amp;gt; the cost of allocating two arrays of the same size, one for a new&lt;br&gt;
&amp;gt; variable called CubeA, and the other for the temporary output of repmat,&lt;br&gt;
&amp;gt; and a memcpy to do the subarray assignment. =A0In the second case, no suc=&lt;br&gt;
h&lt;br&gt;
&amp;gt; thing would ever happen.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; -Peter&lt;br&gt;
&lt;br&gt;
But how accurate is tic/toc?&lt;br&gt;
You're measuring the difference between 3 milliseconds and 0.9&lt;br&gt;
milliseconds.&lt;br&gt;
A better test would be to put the commands within a large for/end&lt;br&gt;
loop, say 10000 iterations:&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; tic;for it=3D1:10000,CubeA(1:300, 1:360, 1) =3D repmat(1000,300,360);end=&lt;br&gt;
,toc&lt;br&gt;
&lt;br&gt;
Elapsed time is 30.724523 seconds.&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; tic;for it=3D1:10000,CubeB{1} =3D repmat(1000,300,360);end,toc&lt;br&gt;
&lt;br&gt;
Elapsed time is 19.932247 seconds.&lt;br&gt;
&lt;br&gt;
Now I believe it.</description>
    </item>
    <item>
      <pubDate>Fri, 11 Jul 2008 21:05:06 -0400</pubDate>
      <title>Re: Why are cell arrays so much faster?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172423#442581</link>
      <author>Matt Fig</author>
      <description>&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; tic; a=repmat(1000,300,360); toc;&lt;br&gt;
&amp;gt; Elapsed time is 0.003951 seconds.&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; tic; b=1000.*ones(300,360); toc;&lt;br&gt;
&amp;gt; Elapsed time is 0.005800 seconds.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; For some reason, I always thought of repmat as being slower &lt;br&gt;
&amp;gt; than matrix operations. Maybe I'll start using it more now &lt;br&gt;
&amp;gt; =).&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Ken&lt;br&gt;
&lt;br&gt;
On my machine: &lt;br&gt;
&lt;br&gt;
tic; a=repmat(1000,300,360); toc;&lt;br&gt;
Elapsed time is 0.035981 seconds.&lt;br&gt;
tic; b=1000.*ones(300,360); toc;&lt;br&gt;
Elapsed time is 0.003114 seconds.</description>
    </item>
    <item>
      <pubDate>Fri, 11 Jul 2008 21:11:01 -0400</pubDate>
      <title>Re: Why are cell arrays so much faster?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172423#442583</link>
      <author>Carlos Adrian Vargas Aguilera</author>
      <description>&lt;br&gt;
&amp;gt; For some reason, I always thought of repmat as being slower &lt;br&gt;
&amp;gt; than matrix operations. Maybe I'll start using it more now &lt;br&gt;
&amp;gt; =).&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Ken&lt;br&gt;
&lt;br&gt;
An once you use it, you'll see that the matrix way and&lt;br&gt;
others like repeated index look so UGLY!&lt;br&gt;
&lt;br&gt;
Carlos</description>
    </item>
    <item>
      <pubDate>Sat, 12 Jul 2008 14:24:02 -0400</pubDate>
      <title>Re: Why are cell arrays so much faster?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172423#442627</link>
      <author>Steve Amphlett</author>
      <description>NZTideMan &amp;lt;mulgor@gmail.com&amp;gt; wrote in message &amp;lt;41094624-&lt;br&gt;
3981-4166-907d-d4d493b87466@8g2000hse.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; On Jul 12, 8:37=A0am, Peter Boettcher &lt;br&gt;
&amp;lt;boettc...@ll.mit.edu&amp;gt; wrote:&lt;br&gt;
&amp;gt; &amp;gt; &quot;David Doria&quot; &amp;lt;daviddo...@gmail.com&amp;gt; writes:&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Below is my experiment - I actually expected the &lt;br&gt;
opposite!&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt;&amp;gt;&amp;gt; tic; CubeA(1:300, 1:360, 1) =3D repmat&lt;br&gt;
(1000,300,360); toc&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Elapsed time is 0.003197 seconds.&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt;&amp;gt;&amp;gt; tic; CubeB{1} =3D repmat(1000,300,360); toc&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Elapsed time is 0.000964 seconds.&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Why is this the case? =A0&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; A cell array element is for all practical purposes the &lt;br&gt;
same as a&lt;br&gt;
&amp;gt; &amp;gt; separate variable. =A0So really this is just the &lt;br&gt;
difference between&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; CubeA(1:300, 1:360, 1) =3D repmat(1000,300,360);&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; and&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; CubeB =3D repmat(1000,300,360);&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; The first includes array indices on the left side, so &lt;br&gt;
it includes&lt;br&gt;
&amp;gt; &amp;gt; subarray assignment into the CubeA variable. =A0The &lt;br&gt;
second simply names&lt;br&gt;
&amp;gt; &amp;gt; the output from repmat.&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; I can't predict or guess at how the just-in-time &lt;br&gt;
compiler or the&lt;br&gt;
&amp;gt; &amp;gt; accelerator will react to the first line. =A0MATLAB &lt;br&gt;
might actually incur&lt;br&gt;
&amp;gt; &amp;gt; the cost of allocating two arrays of the same size, one &lt;br&gt;
for a new&lt;br&gt;
&amp;gt; &amp;gt; variable called CubeA, and the other for the temporary &lt;br&gt;
output of repmat,&lt;br&gt;
&amp;gt; &amp;gt; and a memcpy to do the subarray assignment. =A0In the &lt;br&gt;
second case, no suc=&lt;br&gt;
&amp;gt; h&lt;br&gt;
&amp;gt; &amp;gt; thing would ever happen.&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; -Peter&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; But how accurate is tic/toc?&lt;br&gt;
&amp;gt; You're measuring the difference between 3 milliseconds &lt;br&gt;
and 0.9&lt;br&gt;
&amp;gt; milliseconds.&lt;br&gt;
&amp;gt; A better test would be to put the commands within a large &lt;br&gt;
for/end&lt;br&gt;
&amp;gt; loop, say 10000 iterations:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; tic;for it=3D1:10000,CubeA(1:300, 1:360, 1) =3D repmat&lt;br&gt;
(1000,300,360);end=&lt;br&gt;
&amp;gt; ,toc&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Elapsed time is 30.724523 seconds.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; tic;for it=3D1:10000,CubeB{1} =3D repmat&lt;br&gt;
(1000,300,360);end,toc&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Elapsed time is 19.932247 seconds.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Now I believe it.&lt;br&gt;
&lt;br&gt;
One piece is still missing though.  The OP never said &lt;br&gt;
whether his CubeA was pre-assigned.</description>
    </item>
    <item>
      <pubDate>Sat, 12 Jul 2008 15:08:02 -0400</pubDate>
      <title>Re: Why are cell arrays so much faster?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172423#442630</link>
      <author>David Doria</author>
      <description>Nope, nothing was done before the lines of code I posted.&lt;br&gt;
&lt;br&gt;
Dave</description>
    </item>
  </channel>
</rss>

