{"id":372,"date":"2011-05-20T07:00:59","date_gmt":"2011-05-20T07:00:59","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2011\/05\/20\/more-about-automatic-array-growth-improvements-in-matlab-r2011a\/"},"modified":"2011-05-19T20:18:29","modified_gmt":"2011-05-19T20:18:29","slug":"more-about-automatic-array-growth-improvements-in-matlab-r2011a","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2011\/05\/20\/more-about-automatic-array-growth-improvements-in-matlab-r2011a\/","title":{"rendered":"More about automatic array growth improvements in MATLAB R2011a"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p><a href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/05\/16\/automatic-array-growth-gets-a-lot-faster-in-r2011a\/\">Earlier this week I wrote<\/a> about the improvements in automatic array growth in MATLAB R2011a. The <a href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/05\/16\/automatic-array-growth-gets-a-lot-faster-in-r2011a\/#comments\">posted comments<\/a> made me realize that I should give a few more details so that you can understand all the circumstances under which this improvement\r\n      might benefit you.\r\n   <\/p>\r\n   <p>To illustrate the improvement with a slightly different code fragment than the for-loop I used the other day:<\/p><pre>  y = [];\r\n  while not_done_yet()\r\n      if some_condition()\r\n          y(end+1) = next_value();\r\n      end\r\n  end<\/pre><p>We don't know in advance how big <tt>y<\/tt> is going to be, so it's harder to preallocate space for it.  It would really be nice if I could just write the above code\r\n      and let MATLAB handle all the boring details automatically efficiently.\r\n   <\/p>\r\n   <p>Well, prior to R2011a, MATLAB would all handle all the boring details of array growth automatically but not efficiently. \r\n      Now it also handles array growth efficiently.\r\n   <\/p>\r\n   <p>Here are some more details you might find helpful.<\/p>\r\n   <div>\r\n      <ul>\r\n         <li>Automatic array growth works best for vectors (of any orientation) or when growing along the last dimension of a multidimensional\r\n            array.\r\n         <\/li>\r\n         <li>Automatic array growth works for all numeric types as well as cell arrays and object arrays.<\/li>\r\n         <li>The optimization also helps when you are adding a large number of fields to a struct.<\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>Also, for numeric array types, certain kinds of growth by concatenation are supported. Specifically, the case where a numeric\r\n      array is concatenated with itself is now optimized:\r\n   <\/p><pre>   x = [];\r\n   for k = 1:n\r\n       x = [x k];\r\n   end<\/pre><p>One blog reader <a href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/05\/16\/automatic-array-growth-gets-a-lot-faster-in-r2011a\/#comment-24206\">wondered<\/a> if this optimization means that there might be situations where some very large vectors or arrays might be using twice as\r\n      memory as necessary.\r\n   <\/p>\r\n   <p>The answer is no, not really. MATLAB uses a smarter heuristic than simply doubling the allocated memory space whenever more\r\n      is needed, so for large arrays the worst-case memory \"overallocation\" is much less than a factor of two. I don't intend to\r\n      get into further details here because (a) I don't know them, and (b) I expect that we will continue to tune the heuristic\r\n      and other aspects of automatic array growth with future releases.\r\n   <\/p>\r\n   <p>I'll conclude by repeating my recommendation from earlier this week. If it's trivial to precalculate the final size of a growing\r\n      array before entering a loop, then by all means use preallocation. It's the fastest way to go. But if it's not trivial, and\r\n      if the pattern of growth matches one of the cases described above, then you can consider just letting MATLAB handle the array\r\n      growth for you without worrying about bad performance scaling.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_62a3ee9848424ce7ad4dc64b52f8e314() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='62a3ee9848424ce7ad4dc64b52f8e314 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 62a3ee9848424ce7ad4dc64b52f8e314';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = '';\r\n        copyright = 'Copyright 2011 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_62a3ee9848424ce7ad4dc64b52f8e314()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.12<br><\/p>\r\n<\/div>\r\n<!--\r\n62a3ee9848424ce7ad4dc64b52f8e314 ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/blogs.mathworks.com\/steve\/2011\/05\/16\/automatic-array-growth-gets-a-lot-faster-in-r2011a\/ \r\n% Earlier this week I wrote> about the improvements in automatic array\r\n% growth in MATLAB R2011a. The \r\n% <https:\/\/blogs.mathworks.com\/steve\/2011\/05\/16\/automatic-array-growth-gets-a-lot-faster-in-r2011a\/#comments \r\n% posted comments> made me realize that I\r\n% should give a few more details so that you can understand all the\r\n% circumstances under which this improvement might benefit you.\r\n%\r\n% To illustrate the improvement with a slightly different code fragment\r\n% than the for-loop I used the other day:\r\n%\r\n%    y = [];\r\n%    while not_done_yet()\r\n%        if some_condition()\r\n%            y(end+1) = next_value();\r\n%        end\r\n%    end\r\n%\r\n% We don't know in advance how big |y| is going to be, so it's harder to\r\n% preallocate space for it.  It would really be nice if I could just write\r\n% the above code and let MATLAB handle all the boring details automatically\r\n% efficiently.\r\n%\r\n% Well, prior to R2011a, MATLAB would all handle all the boring details\r\n% of array growth automatically but not efficiently.  Now it also handles\r\n% array growth efficiently.\r\n%\r\n% Here are some more details you might find helpful.\r\n%\r\n% * Automatic array growth works best for vectors (of any orientation)\r\n% or when growing along the last dimension of a multidimensional array.\r\n% * Automatic array growth works for all numeric types as well as\r\n% cell arrays and object arrays.\r\n% * The optimization also helps when you are adding a large number of\r\n% fields to a struct.\r\n%\r\n% Also, for numeric array types, certain kinds of growth by concatenation\r\n% are supported. Specifically, the case where a numeric array is\r\n% concatenated with itself is now optimized:\r\n%\r\n%     x = [];\r\n%     for k = 1:n\r\n%         x = [x k];\r\n%     end\r\n%\r\n% One blog reader \r\n% <https:\/\/blogs.mathworks.com\/steve\/2011\/05\/16\/automatic-array-growth-gets-a-lot-faster-in-r2011a\/#comment-24206 \r\n% wondered> if this optimization means that there might be\r\n% situations where some very large vectors or arrays might be using twice\r\n% as memory as necessary.\r\n%\r\n% The answer is no, not really. MATLAB uses a smarter heuristic than simply\r\n% doubling the allocated memory space whenever more is needed, so for large\r\n% arrays the worst-case memory \"overallocation\" is much less than a factor\r\n% of two. I don't intend to get into further details here because (a) I\r\n% don't know them, and (b) I expect that we will continue to tune the\r\n% heuristic and other aspects of automatic array growth with future\r\n% releases.\r\n%\r\n% I'll conclude by repeating my recommendation from earlier this week. If\r\n% it's trivial to precalculate the final size of a growing array before\r\n% entering a loop, then by all means use preallocation. It's the fastest\r\n% way to go. But if it's not trivial, and if the pattern of growth matches\r\n% one of the cases described above, then you can consider just letting\r\n% MATLAB handle the array growth for you without worrying about bad\r\n% performance scaling.\r\n\r\n##### SOURCE END ##### 62a3ee9848424ce7ad4dc64b52f8e314\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Earlier this week I wrote about the improvements in automatic array growth in MATLAB R2011a. The posted comments made me realize that I should give a few more details so that you can understand... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2011\/05\/20\/more-about-automatic-array-growth-improvements-in-matlab-r2011a\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/372"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/comments?post=372"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/372\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=372"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=372"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}