<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>Undocumented Matlab &#187; Performance</title> <atom:link href="http://undocumentedmatlab.com/blog/tag/performance/feed/" rel="self" type="application/rss+xml" /><link>http://undocumentedmatlab.com</link> <description>Charting Matlab's unsupported hidden underbelly</description> <lastBuildDate>Thu, 17 May 2012 12:01:26 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.1.1</generator> <item><title>Preallocation performance</title><link>http://undocumentedmatlab.com/blog/preallocation-performance/</link> <comments>http://undocumentedmatlab.com/blog/preallocation-performance/#comments</comments> <pubDate>Wed, 16 May 2012 12:14:46 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Memory]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[JIT]]></category> <category><![CDATA[Performance]]></category> <category><![CDATA[Pure Matlab]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2940</guid> <description><![CDATA[Preallocation is a standard Matlab speedup technique. Still, it has several undocumented aspects.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/matrix-processing-performance/' rel='bookmark' title='Matrix processing performance'>Matrix processing performance</a> <small>Matrix operations performance is affected by internal subscriptions in a counter-intuitive way....</small></li><li><a
href='http://undocumentedmatlab.com/blog/performance-scatter-vs-line/' rel='bookmark' title='Performance: scatter vs. line'>Performance: scatter vs. line</a> <small>In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-java-memory-leaks-performance/' rel='bookmark' title='Matlab-Java memory leaks, performance'>Matlab-Java memory leaks, performance</a> <small>Internal fields of Java objects may leak memory - this article explains how to avoid this without sacrificing performance. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/datestr-performance/' rel='bookmark' title='datestr performance'>datestr performance</a> <small>Caching is a simple and very effective means to improve code performance, as demonstrated for the datestr function....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Array <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/matlab_prog/f8-784135.html#f8-793781">preallocation</a> is a standard and quite well-known technique for improving Matlab loop run-time performance. Today&#8217;s article will show that there is more than meets the eye for even such a simple coding technique.</p><p>A note of caution: in the examples that follow, don&#8217;t take any speedup as an expected actual value &#8211; the actual value may well be different on your system. Your mileage may vary. I only mean to display the relative differences between different alternatives.</p><h3 id="problem">The underlying problem</h3><p>Memory management has a direct influence on performance. I have already shown <a
target="_blank" href="http://undocumentedmatlab.com/blog/matlab-java-memory-leaks-performance/">some examples of this</a> in past articles here.</p><p>Preallocation solves a basic problem in simple program loops, where an array is iteratively enlarged with new data (dynamic array growth). Unlike other programming languages (such as C, C++, C# or Java) that use static typing,  Matlab uses dynamic typing. This means that it is natural and easy to modify array size dynamically during program execution. For example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">fibonacci = <span style="color: #080;">&#91;</span><span style="color: #33f;">0</span>, <span style="color: #33f;">1</span><span style="color: #080;">&#93;</span>;
<span style="color: #0000FF;">for</span> idx = <span style="color: #33f;">3</span> <span style="color: #F0F;">:</span> <span style="color: #33f;">100</span>
   fibonacci<span style="color: #080;">&#40;</span>idx<span style="color: #080;">&#41;</span> = fibonacci<span style="color: #080;">&#40;</span>idx-<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> + fibonacci<span style="color: #080;">&#40;</span>idx-<span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>While this may be simple to program, it is not wise with regards to performance. The reason is that whenever an array is resized (typically enlarged), Matlab allocates an entirely new contiguous block of memory for the array, copying the old values from the previous block to the new, then releasing the old block for potential reuse. This operation takes time to execute. In some cases, this reallocation might require accessing virtual memory and page swaps, which would take an even longer time to execute. If the operation is done in a loop, then performance could quickly drop off a cliff.</p><p>The cost of such naïve array growth is theoretically quadratic. This means that multiplying the number of elements by N multiplies the execution time by about N<sup>2</sup>. The reason for this is that Matlab needs to reallocate N times more than before, and each time takes N times longer due to the larger allocation size (the average block size multiplies by N), and N times more data elements to copy from the old to the new memory blocks.</p><p>A very interesting discussion of this phenomenon and various solutions can be found in a <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/102704">newsgroup thread from 2005</a>. Three main solutions were presented: preallocation, selective dynamic growth (<i>allocating headroom</i>) and using cell arrays. The best solution among these in terms of ease of use and performance is preallocation.</p><h3 id="basics">The basics of pre-allocation</h3><p>The basic idea of preallocation is to create a data array in the final expected size before actually starting the processing loop. This saves any reallocations within the loop, since all the data array elements are already available and can be accessed. This solution is useful when the final size is known in advance, as the following snippet illustrates:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Regular dynamic array growth:</span>
<span style="color: #0000FF;">tic</span>
fibonacci = <span style="color: #080;">&#91;</span><span style="color: #33f;">0</span>,<span style="color: #33f;">1</span><span style="color: #080;">&#93;</span>;
<span style="color: #0000FF;">for</span> idx = <span style="color: #33f;">3</span> <span style="color: #F0F;">:</span> <span style="color: #33f;">40000</span>
   fibonacci<span style="color: #080;">&#40;</span>idx<span style="color: #080;">&#41;</span> = fibonacci<span style="color: #080;">&#40;</span>idx-<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> + fibonacci<span style="color: #080;">&#40;</span>idx-<span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">toc</span>
   =&gt; Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.019954</span> seconds.
&nbsp;
<span style="color: #228B22;">% Now use preallocation – 5 times faster than dynamic array growth:</span>
<span style="color: #0000FF;">tic</span>
fibonacci = <span style="color: #0000FF;">zeros</span><span style="color: #080;">&#40;</span><span style="color: #33f;">40000</span>,<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
fibonacci<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>=<span style="color: #33f;">0</span>; fibonacci<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>=<span style="color: #33f;">1</span>;
<span style="color: #0000FF;">for</span> idx = <span style="color: #33f;">3</span> <span style="color: #F0F;">:</span> <span style="color: #33f;">40000</span>, 
   fibonacci<span style="color: #080;">&#40;</span>idx<span style="color: #080;">&#41;</span> = fibonacci<span style="color: #080;">&#40;</span>idx-<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> + fibonacci<span style="color: #080;">&#40;</span>idx-<span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">toc</span>
   =&gt; Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.004132</span> seconds.</pre></div></div><p>On pre-R2011a releases the effect of preallocation is even more pronounced: I got a 35-times speedup on the same machine using Matlab 7.1 (R14 SP3). R2011a (Matlab 7.12) had a dramatic performance boost for such cases in the internal accelerator, so newer releases are much faster in dynamic allocations, but preallocation is still 5 times faster even on R2011a.</p><h3 id="nondeterministic">Non-deterministic pre-allocation</h3><p>Because the effect of preallocation is so dramatic on all Matlab releases, it makes sense to utilize it even in cases where the data array&#8217;s final size is not known in advance. We can do this by estimating an upper bound to the array&#8217;s size, preallocate this large size, and when we&#8217;re done remove any excess elements:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% The final array size is unknown – assume 1Kx3K upper bound (~23MB)</span>
data = <span style="color: #0000FF;">zeros</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1000</span>,<span style="color: #33f;">3000</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% estimated maximal size</span>
numRows = <span style="color: #33f;">0</span>;
numCols = <span style="color: #33f;">0</span>;
<span style="color: #0000FF;">while</span> <span style="color: #080;">&#40;</span>someCondition<span style="color: #080;">&#41;</span>
   colIdx = someValue1;   numCols = <span style="color: #0000FF;">max</span><span style="color: #080;">&#40;</span>numCols,colIdx<span style="color: #080;">&#41;</span>;
   rowIdx = someValue2;   numRows = <span style="color: #0000FF;">max</span><span style="color: #080;">&#40;</span>numRows,rowIdx<span style="color: #080;">&#41;</span>;
   data<span style="color: #080;">&#40;</span>rowIdx,colIdx<span style="color: #080;">&#41;</span> = someOtherValue;
<span style="color: #0000FF;">end</span>
&nbsp;
<span style="color: #228B22;">% Now remove any excess elements</span>
data<span style="color: #080;">&#40;</span><span style="color: #F0F;">:</span>,numCols+<span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #0000FF;">end</span><span style="color: #080;">&#41;</span> = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>;   <span style="color: #228B22;">% remove excess columns</span>
data<span style="color: #080;">&#40;</span>numRows+<span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #0000FF;">end</span>,<span style="color: #F0F;">:</span><span style="color: #080;">&#41;</span> = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>;   <span style="color: #228B22;">% remove excess rows</span></pre></div></div><h3 id="variants">Variants for pre-allocation</h3><p>It turns out that MathWorks&#8217; <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/matlab_prog/f8-784135.html#f8-793795">official suggestion</a> for preallocation, namely using the <i><b>zeros</b></i> function, is not the most efficient:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% MathWorks suggested variant</span>
<span style="color: #0000FF;">clear</span> data1, <span style="color: #0000FF;">tic</span>, data1 = <span style="color: #0000FF;">zeros</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1000</span>,<span style="color: #33f;">3000</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
   =&gt; Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.016907</span> seconds.
&nbsp;
<span style="color: #228B22;">% A much faster alternative - 500 times faster!</span>
<span style="color: #0000FF;">clear</span> data1, <span style="color: #0000FF;">tic</span>, data1<span style="color: #080;">&#40;</span><span style="color: #33f;">1000</span>,<span style="color: #33f;">3000</span><span style="color: #080;">&#41;</span> = <span style="color: #33f;">0</span>; <span style="color: #0000FF;">toc</span>
   =&gt; Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.000034</span> seconds.</pre></div></div><p>The reason for the second variant being so much faster is because it only allocates the memory, without worrying about the internal values (they get a default of 0, <i>false</i> or &#8221;, in case you wondered). On the other hand, <i><b>zeros</b></i> has to place a value in each of the allocated locations, which takes precious time.</p><p>In most cases the differences are immaterial since the preallocation code would only run once in the program, and an extra 17ms isn&#8217;t such a big deal. But in some cases we may have a need to periodically refresh our data, where the extra run-time could quickly accumulate.</p><h3 id="non-default">Pre-allocating non-default values</h3><p>When we need to preallocate a specific value into every data array element, we cannot use Variant #2. The reason is that Variant #2 only sets the very last data element, and all other array elements get assigned the default value (0, ‘’ or false, depending on the array’s data type). In this case, we can use one of the following alternatives (with their associated timings for a 1000&#215;3000 data array):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">scalar = <span style="color: #0000FF;">pi</span>;  <span style="color: #228B22;">% for example...</span>
&nbsp;
data = scalar<span style="color: #080;">&#40;</span><span style="color: #0000FF;">ones</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1000</span>,<span style="color: #33f;">3000</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;           <span style="color: #228B22;">% Variant A: 87.680 msecs</span>
data<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">1000</span>,<span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">3000</span><span style="color: #080;">&#41;</span> = scalar;             <span style="color: #228B22;">% Variant B: 28.646 msecs</span>
data = <span style="color: #0000FF;">repmat</span><span style="color: #080;">&#40;</span>scalar,<span style="color: #33f;">1000</span>,<span style="color: #33f;">3000</span><span style="color: #080;">&#41;</span>;          <span style="color: #228B22;">% Variant C: 17.250 msecs</span>
data = scalar + <span style="color: #0000FF;">zeros</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1000</span>,<span style="color: #33f;">3000</span><span style="color: #080;">&#41;</span>;         <span style="color: #228B22;">% Variant D: 17.168 msecs</span>
data<span style="color: #080;">&#40;</span><span style="color: #33f;">1000</span>,<span style="color: #33f;">3000</span><span style="color: #080;">&#41;</span> = <span style="color: #33f;">0</span>; data = data+scalar;  <span style="color: #228B22;">% Variant E: 16.334 msecs</span></pre></div></div><p>As can be seen, Variants C-E are about twice as fast as Variant B, and 5 times faster than Variant A.</p><h3 id="non-double">Pre-allocating non-double data</h3><p>7.4.5 Preallocating non-double data<br
/> When preallocating an array of a type that is not <i><b>double</b></i>, we should be careful to create it using the desired type, to prevent memory and/or performance inefficiencies. For example, if we need to process a large array of small integers (<i><b>int8</b></i>), it would be inefficient to preallocate an array of doubles and type-convert to/from int8 within every loop iteration. Similarly, it would be inefficient to preallocate the array as a double type and then convert it to int8. Instead, we should create the array as an int8 array in the first place:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Bad idea: allocates 8MB double array, then converts to 1MB int8 array</span>
data = <span style="color: #0000FF;">int8</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">zeros</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1000</span>,<span style="color: #33f;">1000</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;   <span style="color: #228B22;">% 1M elements</span>
   =&gt; Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.008170</span> seconds.
&nbsp;
<span style="color: #228B22;">% Better: directly allocate the array as a 1MB int8 array – x80 faster</span>
data = <span style="color: #0000FF;">zeros</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1000</span>,<span style="color: #33f;">1000</span>,<span style="color:#A020F0;">'int8'</span><span style="color: #080;">&#41;</span>;
   =&gt; Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.000095</span> seconds.</pre></div></div><h3 id="cells">Pre-allocating cell arrays</h3><p>To preallocate a cell-array we can use the cell function (explicit preallocation), or the maximal cell index (implicit preallocation). Explicit preallocation is faster than implicit preallocation, but functionally equivalent (Note: this is contrary to the experience with allocation of numeric arrays and other arrays):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Variant #1: Explicit preallocation of a 1Kx3K cell array</span>
data = <span style="color: #0000FF;">cell</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1000</span>,<span style="color: #33f;">3000</span><span style="color: #080;">&#41;</span>;
   =&gt; Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.004637</span> seconds. 
&nbsp;
<span style="color: #228B22;">% Variant #2: Implicit preallocation – x3 slower than explicit</span>
<span style="color: #0000FF;">clear</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'data'</span><span style="color: #080;">&#41;</span>, data<span style="color: #080;">&#123;</span><span style="color: #33f;">1000</span>,<span style="color: #33f;">3000</span><span style="color: #080;">&#125;</span> = <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span>;
   =&gt; Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.012873</span> seconds.</pre></div></div><h3 id="structs">Pre-allocating arrays of structs</h3><p>To preallocate an array of structs or class objects, we can use the <i><b>repmat</b></i> function to replicate copies of a single data element (explicit preallocation), or just use the maximal data index (implicit preallocation). In this case, unlike the case of cell arrays, implicit preallocation is much faster than explicit preallocation, since the single element does not actually need to be copied multiple times (<a
target="_blank" rel="nofollow" href="http://www.mathworks.com/support/solutions/en/data/1-7S1YKO/">ref</a>):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Variant #1: Explicit preallocation of a 100x300 struct array</span>
element = <span style="color: #0000FF;">struct</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'field1'</span>,<span style="color: #0000FF;">magic</span><span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'field2'</span>,<span style="color: #080;">&#123;</span><span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
data = <span style="color: #0000FF;">repmat</span><span style="color: #080;">&#40;</span>element, <span style="color: #33f;">100</span>, <span style="color: #33f;">300</span><span style="color: #080;">&#41;</span>;
   =&gt; Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.002804</span> seconds. 
&nbsp;
<span style="color: #228B22;">% Variant #2: Implicit preallocation – x7 faster than explicit </span>
element = <span style="color: #0000FF;">struct</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'field1'</span>,<span style="color: #0000FF;">magic</span><span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'field2'</span>,<span style="color: #080;">&#123;</span><span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">clear</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'data'</span><span style="color: #080;">&#41;</span>, data<span style="color: #080;">&#40;</span><span style="color: #33f;">100</span>,<span style="color: #33f;">300</span><span style="color: #080;">&#41;</span> = element;
   =&gt; Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.000429</span> seconds.</pre></div></div><p>When preallocating structs, we can also use a third variant, using the built-in struct feature of replicating the struct when the <i><b>struct</b></i> function is passed a cell array. For example, <code>struct('field1',cell(100,1), 'field2',5)</code> will create 100 structs, each of them having the empty field <i>field1</i> and another field called <i>field2</i> with value 5. Unfortunately, this variant is slower than both of the previous variants.</p><h3 id="objects">Pre-allocating class objects</h3><p>When preallocating in general, ensure that you are using the maximal expected array size. There is no point in preallocating an empty array or an array having a smaller size than the expected maximum, since dynamic memory reallocation will automatically kick-in within the processing-loop. For this reason, <a
target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/2510427/how-to-preallocate-an-array-of-class-in-matlab">do not use</a> the <i>empty()</i> method of class objects to preallocate, but rather <i><b>repmat</b></i> as explained above.</p><p>When using <i><b>repmat</b></i> to replicate class objects, always be careful to note whether you are replicating the object itself (this happens if your class does NOT derive from <i><b>handle</b></i>) or its reference handle (which happens if you derive the class from <i><b>handle</b></i>). If you are replicating objects, then you can safely edit any of their properties independently of each other; but if you replicate references, you are merely using multiple copies of the same reference, so that modifying referenced object #1 will also automatically affect all the other referenced objects. This may or may not be suitable for your particular program requirements, so be careful to check carefully. If you actually need to use independent object copies, you will <a
target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/591495/matlab-preallocate-a-non-numeric-vector#591788">need to call</a> the class constructor multiple times, once for each new independent object.</p><p
/><p>Next week: what if we can&#8217;t avoid dynamic array resizing? &#8211; apparently, all is not lost. Stay tuned&#8230;</p><p><i><br
/> Do you have any similar allocation-related tricks you&#8217;re using? or unexpected differences such as the ones shown above? If so, then please do <a
href="http://UndocumentedMatlab.com/blog/preallocation-performance/#respond">post a comment</a>.<br
/> </i></p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/matrix-processing-performance/' rel='bookmark' title='Matrix processing performance'>Matrix processing performance</a> <small>Matrix operations performance is affected by internal subscriptions in a counter-intuitive way....</small></li><li><a
href='http://undocumentedmatlab.com/blog/performance-scatter-vs-line/' rel='bookmark' title='Performance: scatter vs. line'>Performance: scatter vs. line</a> <small>In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-java-memory-leaks-performance/' rel='bookmark' title='Matlab-Java memory leaks, performance'>Matlab-Java memory leaks, performance</a> <small>Internal fields of Java objects may leak memory - this article explains how to avoid this without sacrificing performance. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/datestr-performance/' rel='bookmark' title='datestr performance'>datestr performance</a> <small>Caching is a simple and very effective means to improve code performance, as demonstrated for the datestr function....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/preallocation-performance/feed/</wfw:commentRss> <slash:comments>9</slash:comments> </item> <item><title>Profiling Matlab memory usage</title><link>http://undocumentedmatlab.com/blog/profiling-matlab-memory-usage/</link> <comments>http://undocumentedmatlab.com/blog/profiling-matlab-memory-usage/#comments</comments> <pubDate>Thu, 01 Mar 2012 00:13:04 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Memory]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Undocumented function]]></category> <category><![CDATA[Performance]]></category> <category><![CDATA[Pure Matlab]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2768</guid> <description><![CDATA[mtic and mtoc were a couple of undocumented features that enabled users of past Matlab releases to easily profile memory usage.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/matlab-java-memory-leaks-performance/' rel='bookmark' title='Matlab-Java memory leaks, performance'>Matlab-Java memory leaks, performance</a> <small>Internal fields of Java objects may leak memory - this article explains how to avoid this without sacrificing performance. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlabs-internal-memory-representation/' rel='bookmark' title='Matlab&#8217;s internal memory representation'>Matlab&#8217;s internal memory representation</a> <small>Matlab's internal memory structure is explored and discussed. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-mex-in-place-editing/' rel='bookmark' title='Matlab mex in-place editing'>Matlab mex in-place editing</a> <small>Editing Matlab arrays in-place can be an important technique for optimizing calculations. This article shows how to do it using Mex. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/types-of-undocumented-matlab-aspects/' rel='bookmark' title='Types of undocumented Matlab aspects'>Types of undocumented Matlab aspects</a> <small>This article lists the different types of undocumented/unsupported/hidden aspects in Matlab...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Anyone who has had experience with real-life applications knows that Memory usage can have a significant impact on the application&#8217;s usability, in aspects such as performance, interactivity, and even (on some lousy memory-management Operating Systems) crashes/hangs.</p><p>In Matlab releases of the past few years, this has been addressed by expanding the information reported by the built-in <i><b>memory</b></i> function. In addition, an undocumented feature was added to the Matlab Profiler that <a
target="_blank" href="http://undocumentedmatlab.com/blog/undocumented-profiler-options/">enables monitoring</a> memory usage.</p><p><center><div
class="wp-caption aligncenter" style="width: 460px"><br
/> <img
title="Profile report with memory &amp; JIT info" src="http://undocumentedmatlab.com/images/profile2d_450.png" alt="Profile report with memory &amp; JIT info" width="450" /><img
title="Profile report with memory &amp; JIT info" src="http://undocumentedmatlab.com/images/profile2c_450.png" alt="Profile report with memory &amp; JIT info" width="450" /></p><p><img
title="Profile report with memory &amp; JIT info" src="http://undocumentedmatlab.com/images/profile2.png" alt="Profile report with memory &amp; JIT info" width="416" /><br
/><p
class="wp-caption-text">Profile report with memory &amp; JIT info</p></div></center></p><p>In Matlab release R2008a (but not on newer releases) we could also use a nifty parameter of the undocumented <a
target="_blank" href="http://undocumentedmatlab.com/blog/undocumented-feature-function/"><i><b>feature</b></i> function</a>:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; feature mtic; a=<span style="color: #0000FF;">ones</span><span style="color: #080;">&#40;</span><span style="color: #33f;">100</span><span style="color: #080;">&#41;</span>; feature mtoc
<span style="color: #0000FF;">ans</span> = 
      TotalAllocated<span style="color: #F0F;">:</span> <span style="color: #33f;">84216</span>
          TotalFreed<span style="color: #F0F;">:</span> <span style="color: #33f;">2584</span>
    LargestAllocated<span style="color: #F0F;">:</span> <span style="color: #33f;">80000</span>
           NumAllocs<span style="color: #F0F;">:</span> <span style="color: #33f;">56</span>
            NumFrees<span style="color: #F0F;">:</span> <span style="color: #33f;">43</span>
                Peak<span style="color: #F0F;">:</span> <span style="color: #33f;">81640</span></pre></div></div><p>As can easily be seen in this example, allocating 100<sup>2</sup> doubles requires 80000 bytes of allocation, plus some 4KB others that were allocated (and 2KB freed) within the function <i><b>ones</b></i>. Running the same code line again gives a very similar result, but now there are 80000 more bytes freed when the matrix <code>a</code> is overwritten:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; feature mtic; a=<span style="color: #0000FF;">ones</span><span style="color: #080;">&#40;</span><span style="color: #33f;">100</span><span style="color: #080;">&#41;</span>; feature mtoc
<span style="color: #0000FF;">ans</span> = 
      TotalAllocated<span style="color: #F0F;">:</span> <span style="color: #33f;">84120</span>
          TotalFreed<span style="color: #F0F;">:</span> <span style="color: #33f;">82760</span>
    LargestAllocated<span style="color: #F0F;">:</span> <span style="color: #33f;">80000</span>
           NumAllocs<span style="color: #F0F;">:</span> <span style="color: #33f;">54</span>
            NumFrees<span style="color: #F0F;">:</span> <span style="color: #33f;">49</span>
                Peak<span style="color: #F0F;">:</span> <span style="color: #33f;">81328</span></pre></div></div><p>This is pretty informative and very handy for debugging memory bottlenecks. Unfortunately, starting in R2008b, features mtic and mtoc are no longer supported <i>&#8220;under the current <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/support/tech-notes/1100/1106.html">memory manager</a>&#8220;</i>. Sometime around 2010 the mtic and mtoc features were completely removed. Users of R2008b and newer releases therefore need to use the internal structs returned by the <i><b>memory</b></i> function, and/or use the profiler&#8217;s memory-monitoring feature. If you ask me, using mtic/mtoc was much simpler and easier. I for one miss these features.</p><p>In a related matter, if we wish to monitor Java&#8217;s memory used within Matlab, we are in a bind, because there are no built-in tools to help us. there are several JVM switches that can be turned on in the <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/support/solutions/en/data/1-18I2C/"><i>java.opts</i></a> file: -Xrunhprof[:help]|[:option=value,...], -Xprof, -Xrunprof, -XX:+PrintClassHistogram <a
target="_blank" rel="nofollow" href="http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html">and so on</a>. There are several memory-monitoring (so-called &#8220;heap-walking&#8221;) tools: the standard JDK jconsole, jmap, jhat and jvisualvm (with its useful plugins) provide good basic coverage. MathWorks has <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/support/solutions/en/data/1-3L4JU7/">posted</a> a tutorial on using jconsole with Matlab. There are a number of other third-party tools such as <a
target="_blank" rel="nofollow" href="http://www.khelekore.org/jmp/">JMP</a> (for JVMs 1.5 and earlier) or <a
target="_blank" rel="nofollow" href="http://www.khelekore.org/jmp/tijmp/">TIJMP</a> (for JVM 1.6). Within Matlab, we can use utilities such as <a
target="_blank" rel="nofollow" href="http://www.javamex.com/classmexer/">Classmexer</a> to estimate a particular object&#8217;s size (both shallow and deep referencing), or use <code>java.lang.Runtime.getRuntime()</code>&#8216;s methods (<i>maxMemory(), freeMemory()</i> and <i>totalMemory()</i>) to monitor overall Java memory (<a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/296813#797410">sample usage</a>).</p><p>Specifically in R2011b (but in no other release), we can also use a built-in Java memory monitor. Unfortunately, this simple and yet useful memory monitor was removed in R2012a (or maybe it was just moved to another package and I haven&#8217;t found out where&#8230; <i>yet</i>&#8230;):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">com.<span style="">mathworks</span>.<span style="">xwidgets</span>.<span style="">JavaMemoryMonitor</span>.<span style="">invoke</span></pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 230px"><img
alt="Matlab R2011b's Java memory monitor" src="http://UndocumentedMatlab.com/images/Java_Memory_Monitor.png" title="Matlab R2011b's Java memory monitor" width="159" /><p
class="wp-caption-text">Matlab R2011b's Java memory monitor</p></div></center></p><p>As I have already noted quite often, using undocumented Matlab features and functions carries the risk that they will not be supported in some future Matlab release. Today&#8217;s article is a case in point.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/matlab-java-memory-leaks-performance/' rel='bookmark' title='Matlab-Java memory leaks, performance'>Matlab-Java memory leaks, performance</a> <small>Internal fields of Java objects may leak memory - this article explains how to avoid this without sacrificing performance. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlabs-internal-memory-representation/' rel='bookmark' title='Matlab&#8217;s internal memory representation'>Matlab&#8217;s internal memory representation</a> <small>Matlab's internal memory structure is explored and discussed. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-mex-in-place-editing/' rel='bookmark' title='Matlab mex in-place editing'>Matlab mex in-place editing</a> <small>Editing Matlab arrays in-place can be an important technique for optimizing calculations. This article shows how to do it using Mex. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/types-of-undocumented-matlab-aspects/' rel='bookmark' title='Types of undocumented Matlab aspects'>Types of undocumented Matlab aspects</a> <small>This article lists the different types of undocumented/unsupported/hidden aspects in Matlab...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/profiling-matlab-memory-usage/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Matlab mex in-place editing</title><link>http://undocumentedmatlab.com/blog/matlab-mex-in-place-editing/</link> <comments>http://undocumentedmatlab.com/blog/matlab-mex-in-place-editing/#comments</comments> <pubDate>Wed, 08 Feb 2012 17:00:25 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Guest bloggers]]></category> <category><![CDATA[High risk of breaking in future versions]]></category> <category><![CDATA[Memory]]></category> <category><![CDATA[Mex]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Performance]]></category> <category><![CDATA[Peter Li]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2699</guid> <description><![CDATA[Editing Matlab arrays in-place can be an important technique for optimizing calculations. This article shows how to do it using Mex.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/matlabs-internal-memory-representation/' rel='bookmark' title='Matlab&#8217;s internal memory representation'>Matlab&#8217;s internal memory representation</a> <small>Matlab's internal memory structure is explored and discussed. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/profiling-matlab-memory-usage/' rel='bookmark' title='Profiling Matlab memory usage'>Profiling Matlab memory usage</a> <small>mtic and mtoc were a couple of undocumented features that enabled users of past Matlab releases to easily profile memory usage. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-java-memory-leaks-performance/' rel='bookmark' title='Matlab-Java memory leaks, performance'>Matlab-Java memory leaks, performance</a> <small>Internal fields of Java objects may leak memory - this article explains how to avoid this without sacrificing performance. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/jmi-java-to-matlab-interface/' rel='bookmark' title='JMI &#8211; Java-to-Matlab Interface'>JMI &#8211; Java-to-Matlab Interface</a> <small>JMI enables calling Matlab functions from within Java. This article explains JMI's core functionality....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p><i>I would like to welcome Matlab Mex power-user <a
target="_blank" rel="nofollow" href="http://absurdlycertain.blogspot.com/">Peter Li</a> to a first in a short series of articles about undocumented aspects of Mex programing</i></p><p>Editing Matlab arrays in-place can be an important technique for optimizing calculations, especially when handling data that use large blocks of memory.  The Matlab language itself has some <a
target="_blank" rel="nofollow" href="http://blogs.mathworks.com/loren/2007/03/22/in-place-operations-on-data/">limited support for in-place editing</a>, but when we are really concerned with speed we often turn to writing C/C++ extensions using the Mex interface.  Unfortunately, editing arrays in-place from Mex extensions is not officially supported in Matlab, and doing it incorrectly can cause data inconsistencies or can even cause Matlab to crash.  In this article, I will introduce the problem and show a simple solution that exhibit the basic implementation details of Matlab&#8217;s internal copy-on-write mechanism.</p><h3 id="Motivation">Why edit in-place?</h3><p>To demonstrate the techniques in this article, I use the <i>fast_median</i> function, which is part of <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/29453-nthelement">my nth_element package</a> on Matlab&#8217;s File Exchange.  You can download the package and play with the code if you want.  The examples are fairly self-explanatory, so if you do not want to try the code you should be okay just following along.</p><p>Let us try a few function calls to see how editing in-place can save time and memory:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; A = <span style="color: #0000FF;">rand</span><span style="color: #080;">&#40;</span><span style="color: #33f;">100000000</span>, <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
&gt;&gt; <span style="color: #0000FF;">tic</span>; <span style="color: #0000FF;">median</span><span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>    
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">4.122654</span> seconds.
&nbsp;
&gt;&gt; <span style="color: #0000FF;">tic</span>; fast_median<span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">1.646448</span> seconds.
&nbsp;
&gt;&gt; <span style="color: #0000FF;">tic</span>; fast_median_ip<span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.927898</span> seconds.</pre></div></div><p>If you try running this, be careful not to make A too large; tune the example according to the memory available on your system. In terms of the execution time for the different functions, your mileage may vary depending on factors such as: your system, what Matlab version you are running, and whether your test data is arranged in a single vector or a multicolumn array.</p><p>This example illustrates a few general points: first, <i>fast_median</i> is significantly faster than Matlab&#8217;s <i><b>native</b></i> median function. This is because <i>fast_median</i> uses a more efficient algorithm; see the nth_element page for more details.  Besides being a shameless plug, this demonstrates why we might want to write a Mex function in the first place: rewriting the median function in pure Matlab would be slow, but using C++ we can significantly improve on the status quo.</p><p>The second point is that the in-place version, <i>fast_median_ip</i>, yields an additional speed improvement.  What is the difference?  Let us look behind the scenes; here are the CPU and memory traces from my system monitor after running the above:</p><p><center><div
class="wp-caption alignleft" style="width: 387px"><img
alt="Memory and CPU usage for median() vs. fast_median_ip()" src="http://UndocumentedMatlab.com/images/median_vs_fast_median_ip.png" title="Memory and CPU usage for median() vs. fast_median_ip()" width="377" height="425"/><p
class="wp-caption-text">Memory and CPU usage for <i><b>median</b></i> vs. <i>fast_median_ip</i></p></div></center></p><p>You can see four spikes in CPU use, and some associated changes in memory allocation:</p><p>The first spike in CPU is when we created the test data vector; memory use also steps up at that time.</p><p>The second CPU spike is the largest; that is Matlab&#8217;s median function.  You can see that over that period memory use stepped up again, and then stepped back down; the median function makes a copy of the entire input data, and then throws its copy away when it is finished; this is expensive in terms of time and resources.</p><p>The <i>fast_median</i> function is the next CPU spike; it has a similar step up and down in memory use, but it is much faster.</p><p>Finally, in the case of <i>fast_median_ip</i> we see something different; there is a spike in CPU use, but memory use stays flat; the in-place version is faster and more memory efficient because it does not make a copy of the input data.</p><div
class="" style="width: 100%; overflow: auto;"></div><p>There is another important difference with the in-place version; it modifies its input array.  This can be demonstrated simply:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; A = randi<span style="color: #080;">&#40;</span><span style="color: #33f;">100</span>, <span style="color: #080;">&#91;</span><span style="color: #33f;">10</span> <span style="color: #33f;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
&gt;&gt; A'
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">39</span>    <span style="color: #33f;">42</span>    <span style="color: #33f;">98</span>    <span style="color: #33f;">25</span>    <span style="color: #33f;">64</span>    <span style="color: #33f;">75</span>     <span style="color: #33f;">6</span>    <span style="color: #33f;">56</span>    <span style="color: #33f;">71</span>    <span style="color: #33f;">89</span>
&nbsp;
&gt;&gt; <span style="color: #0000FF;">median</span><span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">60</span>
&nbsp;
&gt;&gt; fast_median<span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">60</span>
&gt;&gt; A'
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">39</span>    <span style="color: #33f;">42</span>    <span style="color: #33f;">98</span>    <span style="color: #33f;">25</span>    <span style="color: #33f;">64</span>    <span style="color: #33f;">75</span>     <span style="color: #33f;">6</span>    <span style="color: #33f;">56</span>    <span style="color: #33f;">71</span>    <span style="color: #33f;">89</span>
&nbsp;
&gt;&gt; fast_median_ip<span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">60</span>
&gt;&gt; A'
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">39</span>     <span style="color: #33f;">6</span>    <span style="color: #33f;">25</span>    <span style="color: #33f;">42</span>    <span style="color: #33f;">56</span>    <span style="color: #33f;">64</span>    <span style="color: #33f;">75</span>    <span style="color: #33f;">71</span>    <span style="color: #33f;">98</span>    <span style="color: #33f;">89</span></pre></div></div><p>As you can see, all three methods get the same answer, but <i><b>median</b></i> and <i>fast_median</i> do not modify A in the workspace, whereas after running <i>fast_median_ip</i>, input array A has changed.  This is how the in-place method is able to run without using new memory; it operates on the existing array rather than making a copy.</p><h3 id="Pitfalls">Pitfalls with in-place editing</h3><p>Modifying a function&#8217;s input is common in many languages, but in Matlab there are only a few special conditions under which this is officially sanctioned.  This is not necessarily a bad thing; many people feel that modifying input data is bad programming practice and makes code harder to maintain.  But as we have shown, it can be an important capability to have if speed and memory use are critical to an application.</p><p>Given that in-place editing is not officially supported in Matlab Mex extensions, what do we have to do to make it work?  Let us look at the normal, input-copying <i>fast_median</i> function as a starting point:</p><div
class="wp_syntax"><div
class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> mexFunction<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> nlhs, mxArray <span style="color: #000040;">*</span>plhs<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> nrhs, <span style="color: #0000ff;">const</span> mxArray <span style="color: #000040;">*</span>prhs<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
   mxArray <span style="color: #000040;">*</span>incopy <span style="color: #000080;">=</span> mxDuplicateArray<span style="color: #008000;">&#40;</span>prhs<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   plhs<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> run_fast_median<span style="color: #008000;">&#40;</span>incopy<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p>This is a pretty simple function (I have taken out a few lines of boiler plate input checking to keep things clean).  It relies on helper function <i>run_fast_median</i> to do the actual calculation, so the only real logic here is copying the input array <code>prhs[0]</code>.  Importantly, <i>run_fast_median</i> edits its inputs in-place, so the call to <i>mxDuplicateArray</i> ensures that the Mex function is overall well behaved, i.e. that it does not change its inputs.</p><p>Who wants to be well behaved though?  Can we save time and memory just by taking out the input duplication step?  Let us try it:</p><div
class="wp_syntax"><div
class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> mexFunction<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> nlhs, mxArray <span style="color: #000040;">*</span>plhs<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> nrhs, <span style="color: #0000ff;">const</span> mxArray <span style="color: #000040;">*</span>prhs<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
   plhs<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> run_fast_median<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const_cast</span><span style="color: #000080;">&lt;</span>mxarray <span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>prhs<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>  <span style="color: #666666;">// &lt;/mxarray&gt;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p>Very bad behavior; note that we cast the original <code>const mxArray*</code> input to a <code>mxArray*</code> so that the compiler will let us mess with it; naughty.</p><p>But does this accomplish edit in-place for <i>fast_median</i>?  Be sure to save any work you have open and then try it:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; mex fast_median_tweaked.<span style="">cpp</span>
&gt;&gt; A = randi<span style="color: #080;">&#40;</span><span style="color: #33f;">100</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">10</span> <span style="color: #33f;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
&gt;&gt; fast_median_tweaked<span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">43</span></pre></div></div><p>Hmm, it looks like this worked fine.  But in fact there are subtle problems:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; A = randi<span style="color: #080;">&#40;</span><span style="color: #33f;">100</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">10</span> <span style="color: #33f;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
&gt;&gt; A'
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">65</span>    <span style="color: #33f;">92</span>    <span style="color: #33f;">14</span>    <span style="color: #33f;">26</span>    <span style="color: #33f;">41</span>     <span style="color: #33f;">2</span>    <span style="color: #33f;">45</span>    <span style="color: #33f;">85</span>    <span style="color: #33f;">53</span>     <span style="color: #33f;">2</span>
&gt;&gt; B = A;
&gt;&gt; B'
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">65</span>    <span style="color: #33f;">92</span>    <span style="color: #33f;">14</span>    <span style="color: #33f;">26</span>    <span style="color: #33f;">41</span>     <span style="color: #33f;">2</span>    <span style="color: #33f;">45</span>    <span style="color: #33f;">85</span>    <span style="color: #33f;">53</span>     <span style="color: #33f;">2</span>
&nbsp;
&gt;&gt; fast_median_tweaked<span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">43</span>
&gt;&gt; A'
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">2</span>     <span style="color: #33f;">2</span>    <span style="color: #33f;">41</span>    <span style="color: #33f;">26</span>    <span style="color: #33f;">14</span>    <span style="color: #33f;">45</span>    <span style="color: #33f;">65</span>    <span style="color: #33f;">85</span>    <span style="color: #33f;">53</span>    <span style="color: #33f;">92</span>
&gt;&gt; B'
<span style="color: #0000FF;">ans</span> = <span style="color: #33f;">2</span>     <span style="color: #33f;">2</span>    <span style="color: #33f;">41</span>    <span style="color: #33f;">26</span>    <span style="color: #33f;">14</span>    <span style="color: #33f;">45</span>    <span style="color: #33f;">65</span>    <span style="color: #33f;">85</span>    <span style="color: #33f;">53</span>    <span style="color: #33f;">92</span></pre></div></div><p>Uhoh, spooky; we expected that running <i>fast_median_tweaked</i> would change input A, but somehow it has also changed B, even though B is supposed to be an independent copy.  Not good.  In fact, under some conditions this kind of uncontrolled editing in-place can crash the entire Matlab environment with a segfault.  What is going on?</p><h3 id="COW">Matlab&#8217;s copy-on-write mechanism</h3><p>The answer is that our simple attempt to edit in-place conflicts with Matlab&#8217;s internal copy-on-write mechanism.  Copy-on-write is an optimization built into Matlab to help avoid expensive copying of variables in memory (actually similar to what we are trying to accomplish with edit in-place).  We can see copy-on-write in action with some simple tests:</p><div
class="wp-caption alignright" style="width: 403px"><img
alt="Matlab's Copy-on-Write memory and CPU usage" src="http://UndocumentedMatlab.com/images/copy-on-write.png" title="Matlab's Copy-on-Write memory and CPU usage" width="393" height="466"/><p
class="wp-caption-text">Matlab's Copy-on-Write memory and CPU usage</p></div><div><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Test #1: update, then copy</span>
&gt;&gt; <span style="color: #0000FF;">tic</span>; A = <span style="color: #0000FF;">zeros</span><span style="color: #080;">&#40;</span><span style="color: #33f;">100000000</span>, <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.588937</span> seconds.
&gt;&gt; <span style="color: #0000FF;">tic</span>; A<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> = <span style="color: #33f;">0</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.000008</span> seconds.
&gt;&gt; <span style="color: #0000FF;">tic</span>; B = A; <span style="color: #0000FF;">toc</span>   
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.000020</span> seconds.
&nbsp;
<span style="color: #228B22;">% Test #2: copy, then update</span>
&gt;&gt; <span style="color: #0000FF;">clear</span>
&gt;&gt; <span style="color: #0000FF;">tic</span>; A = <span style="color: #0000FF;">zeros</span><span style="color: #080;">&#40;</span><span style="color: #33f;">100000000</span>, <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.588937</span> seconds.
&gt;&gt; <span style="color: #0000FF;">tic</span>; B = A; <span style="color: #0000FF;">toc</span>   
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.000020</span> seconds.
&gt;&gt; <span style="color: #0000FF;">tic</span>; A<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> = <span style="color: #33f;">0</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.678160</span> seconds.</pre></div></div></div><p>In the first set of operations, time and memory are used to create A, but updating A and &#8220;copying&#8221; A into B take no memory and essentially no time.  This may come as a surprise since supposedly we have made an independent copy of A in B; why does creating B take no time or memory when A is clearly a large, expensive block?</p><p>The second set of operations makes things more clear.  In this case, we again create A and then copy it to B; again this operation is fast and cheap.  But assigning into A at this point takes time and consumes a new block of memory, even though we are only assigning into a single index of A.  This is copy-on-write: Matlab tries to save you from copying large blocks of memory unless you need to.  So when you first assign B to equal A, nothing is copied; the variable B is simply set to point to the same memory location already used by A.  Only after you try to change A (or B), does Matlab decide that you really need to have two copies of the large array.</p><p>There are some additional tricks Matlab does with copy-on-write.  Here is another example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">clear</span>
&gt;&gt; <span style="color: #0000FF;">tic</span>; A<span style="color: #080;">&#123;</span><span style="color: #33f;">1</span><span style="color: #080;">&#125;</span> = <span style="color: #0000FF;">zeros</span><span style="color: #080;">&#40;</span><span style="color: #33f;">100000000</span>, <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.573240</span> seconds.
&gt;&gt; <span style="color: #0000FF;">tic</span>; A<span style="color: #080;">&#123;</span><span style="color: #33f;">2</span><span style="color: #080;">&#125;</span> = <span style="color: #0000FF;">zeros</span><span style="color: #080;">&#40;</span><span style="color: #33f;">100000000</span>, <span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.560369</span> seconds.
&nbsp;
&gt;&gt; <span style="color: #0000FF;">tic</span>; B = A; <span style="color: #0000FF;">toc</span>                     
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.000016</span> seconds.
&nbsp;
&gt;&gt; <span style="color: #0000FF;">tic</span>; A<span style="color: #080;">&#123;</span><span style="color: #33f;">1</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> = <span style="color: #33f;">0</span>; <span style="color: #0000FF;">toc</span>               
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.690690</span> seconds.
&gt;&gt; <span style="color: #0000FF;">tic</span>; A<span style="color: #080;">&#123;</span><span style="color: #33f;">2</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> = <span style="color: #33f;">0</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.695758</span> seconds.
&nbsp;
&gt;&gt; <span style="color: #0000FF;">tic</span>; A<span style="color: #080;">&#123;</span><span style="color: #33f;">1</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> = <span style="color: #33f;">0</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.000011</span> seconds.
&gt;&gt; <span style="color: #0000FF;">tic</span>; A<span style="color: #080;">&#123;</span><span style="color: #33f;">2</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> = <span style="color: #33f;">0</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.000004</span> seconds.</pre></div></div><p>This shows that for the purposes of copy-on-write, different elements of cell array A are treated independently.  When we assign B equal to A, nothing is copied.  Then when we change any part of A{1}, that whole element must be copied over.  When we subsequently change A{2}, that whole element must also be copied over; it was not copied earlier.  At this point, A and B are truly independent of each other, as both elements have experienced copy-on-write, so further assignments into either A or B are fast and require no additional memory.</p><p>Try playing with some struct arrays and you will find that copy-on-write also works independently for the elements of structs.</p><h3 id="mxUnshareArray">Reconciling edit in-place with copy-on-write: mxUnshareArray</h3><p>Now it is clear why we cannot simply edit arrays in-place from Mex functions; not only is it naughty, it fundamentally conflicts with copy-on-write.  Naively changing an array in-place can inadvertently change other variables that are still waiting for a copy-on-write, as we saw above when <i>fast_median_tweaked</i> inadvertently changed B in the workspace. This is, in the best case, an unmaintainable mess.  Under more aggressive in-place editing, it can cause Matlab to crash with a segfault.</p><p>Luckily, there is a simple solution, although it requires calling internal, undocumented Matlab functions.</p><p>Essentially what we need is a Mex function that can be run on a Matlab array that will do the following:</p><ol><li>Check whether the current array is sharing data with any other arrays that are waiting for copy-on-write.</li><li>If the array is shared, it must be unshared; the underlying memory must be copied and all the relevant pointers need to be fixed so that the array we want to work on is no longer accessible by anyone else.</li><li>If the array is not currently shared, simply proceed; the whole point is to avoid copying memory if we do not need to, so that we can benefit from the efficiencies of edit in-place.</li></ol><p>If you think about it, this is exactly the operation that Matlab needs to run internally when it is deciding whether an assignment operation requires a copy-on-write.  So it should come as no surprise that such a Mex function already exists in the form of a Matlab internal called <i>mxUnshareArray</i>.  Here is how you use it:</p><div
class="wp_syntax"><div
class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">extern</span> <span style="color: #FF0000;">&quot;C&quot;</span> <span style="color: #0000ff;">bool</span> mxUnshareArray<span style="color: #008000;">&#40;</span>mxArray <span style="color: #000040;">*</span>array_ptr, <span style="color: #0000ff;">bool</span> noDeepCopy<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">void</span> mexFunction<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> nlhs, mxArray <span style="color: #000040;">*</span>plhs<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> nrhs, <span style="color: #0000ff;">const</span> mxArray <span style="color: #000040;">*</span>prhs<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
   mxUnshareArray<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const_cast</span><span style="color: #000080;">&lt;</span>mxarray <span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>prhs<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>, <span style="color: #0000ff;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>  <span style="color: #666666;">//&lt;/mxarray&gt;</span>
   plhs<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> run_fast_median<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const_cast</span><span style="color: #000080;">&lt;</span>mxarray <span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>prhs<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>  <span style="color: #666666;">//&lt;/mxarray&gt;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p>This is the method actually used by <i>fast_median_ip</i> to efficiently edit in-place without risking conflicts with copy-on-write.  Of course, if the array turns out to need to be unshared, then you do not get the benefit of edit in-place because the memory ends up getting copied.  But at least things are safe and you get the in-place benefit as long as the input array is not being shared.</p><h3 id="Extra">Further topics</h3><p>The method shown here should allow you to edit normal Matlab numerical or character arrays in-place from Mex functions safely.  For a Mex function in C rather than C++, omit the &#8220;C&#8221; in the <code>extern</code> declaration and of course you will have to use C-style casting rather than <code>const_cast</code>.  If you need to edit cell or struct arrays in-place, or especially if you need to edit subsections of shared cell or struct arrays safely and efficiently while leaving the rest of the data shared, then you will need a few more tricks.  A good place to get started is <a
target="_blank" rel="nofollow" href="http://www.mk.tu-berlin.de/Members/Benjamin/mex_sharedArrays">this article by Benjamin Schubert</a>.</p><p>Unfortunately, over the last few years Mathworks seems to have decided to make it more difficult for users to access these kinds of internal methods to make our code more efficient.  So be aware of the risk that in some future version of Matlab this method will no longer work in its current form.</p><p>Ultimately much of what is known about <i>mxUnshareArray</i> as well as the internal implementation details of how Matlab keeps track of which arrays are shared goes back to the work of Peter Boettcher, particularly his <a
target="_blank" rel="nofollow" href="http://groups.google.com/group/comp.soft-sys.matlab/browse_thread/thread/c241d8821fb90275/">headerdump.c utility</a>.  Unfortunately, it appears that HeaderDump fails with Matlab releases >=R2010a, as Mathworks have changed some of the internal memory formats &#8211; perhaps some smart reader can pick up the work and adapt HeaderDump to the new memory format.</p><p>In a future article, I hope to discuss headerdump.c and its relevance for copy-on-write and edit in-place, and some other related tools for the latest Matlab releases that do not support HeaderDump.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/matlabs-internal-memory-representation/' rel='bookmark' title='Matlab&#8217;s internal memory representation'>Matlab&#8217;s internal memory representation</a> <small>Matlab's internal memory structure is explored and discussed. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/profiling-matlab-memory-usage/' rel='bookmark' title='Profiling Matlab memory usage'>Profiling Matlab memory usage</a> <small>mtic and mtoc were a couple of undocumented features that enabled users of past Matlab releases to easily profile memory usage. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-java-memory-leaks-performance/' rel='bookmark' title='Matlab-Java memory leaks, performance'>Matlab-Java memory leaks, performance</a> <small>Internal fields of Java objects may leak memory - this article explains how to avoid this without sacrificing performance. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/jmi-java-to-matlab-interface/' rel='bookmark' title='JMI &#8211; Java-to-Matlab Interface'>JMI &#8211; Java-to-Matlab Interface</a> <small>JMI enables calling Matlab functions from within Java. This article explains JMI's core functionality....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/matlab-mex-in-place-editing/feed/</wfw:commentRss> <slash:comments>7</slash:comments> </item> <item><title>Matlab-Java memory leaks, performance</title><link>http://undocumentedmatlab.com/blog/matlab-java-memory-leaks-performance/</link> <comments>http://undocumentedmatlab.com/blog/matlab-java-memory-leaks-performance/#comments</comments> <pubDate>Fri, 20 Jan 2012 00:56:10 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Java]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Memory]]></category> <category><![CDATA[Semi-documented feature]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Callbacks]]></category> <category><![CDATA[Performance]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2665</guid> <description><![CDATA[Internal fields of Java objects may leak memory - this article explains how to avoid this without sacrificing performance.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/profiling-matlab-memory-usage/' rel='bookmark' title='Profiling Matlab memory usage'>Profiling Matlab memory usage</a> <small>mtic and mtoc were a couple of undocumented features that enabled users of past Matlab releases to easily profile memory usage. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/preallocation-performance/' rel='bookmark' title='Preallocation performance'>Preallocation performance</a> <small>Preallocation is a standard Matlab speedup technique. Still, it has several undocumented aspects. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/performance-scatter-vs-line/' rel='bookmark' title='Performance: scatter vs. line'>Performance: scatter vs. line</a> <small>In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-java-interface-using-static-control/' rel='bookmark' title='Matlab-Java interface using a static control'>Matlab-Java interface using a static control</a> <small>The switchyard function design pattern can be very useful when setting Matlab callbacks to Java GUI controls. This article explains why and how....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>There are several ways of retrieving information from a Java object into Matlab. On the face of it, all these methods look similar. But it turns out that there are important differences between them in terms of memory leakage and performance.</p><h3 id="Problem">The problem: &#8220;Matlab crashes&#8221; &#8211; now go figure&#8230;</h3><p>A client of one of my Matlab programs recently complained that Matlab crashes after several hours of extensive use of the program. The problem looked like something that is memory related (messages such as Matlab&#8217;s out-of-memory error or Java&#8217;s heap-space error). Apparently this happens even on 64-bit systems having lots of memory, where memory should never be a problem.</p><p>Well, we know that this is only in theory, but in practice Matlab&#8217;s internal memory management has problems that occasionally lead to such crashes. This is one of the reasons, by the way, that recent Matlab releases have added the preference option of increasing the default Java heap space (the previous way to do this was <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/support/solutions/en/data/1-18I2C/">a bit complex</a>). Still, even with a high Java heap space setting and lots of RAM, Matlab crashed after using my program for several hours.</p><p>Not pleasant at all, even a bit of an embarrassment for me. I&#8217;m used to crashing Matlab, but only as a result of my playing around with the internals &#8211; I would hate it to happen to my clients.</p><h3 id="Finding">Finding the leak</h3><p>While we can do little with Matlab&#8217;s internal memory manager, I started searching for the exact location of the memory leak and then to find a way to overcome it. I&#8217;ll save readers the description about the grueling task of finding out exactly where the memory leak occurred in a program that has thousands of lines of code and where events get fired asynchronously on a constant basis. <a
target="_blank" href="http://undocumentedmatlab.com/blog/undocumented-profiler-options/">Matlab Profiler&#8217;s undocumented memory profiling option</a> helped me quite a bit, as well as lots of intuition and trial-and-error. Detecting memory leak is never easy, and I consider myself somewhat lucky this time to have both detected the leak source and a workaround.</p><p>It turned out that the leakage happens in a callback that gets invoked multiple times per second by a Java object (see related articles <a
target="_blank" href="http://undocumentedmatlab.com/blog/uicontrol-callbacks/">here</a> and <a
target="_blank" href="http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/">here</a>). Each time the Matlab callback function is invoked, it reads the event information from the supplied Java event-data (the callback&#8217;s second input parameter). Apparently, about 1KB of memory gets leaked whenever this event-data is being read. This may appear a very small leak, but multiply this by some 50-100K callback invocations per hour and you get a leakage of 50-100MB/hour. Not a small leak at all; more of a flood you could say&#8230;</p><h3 id="get">Using <i><b>get</b>()</i></h3><p>The leakage culprit turned out to be the following code snippet:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% 160 uSecs per call, with memory leak</span>
eventData  = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>hEventData,<span style="color: #080;">&#123;</span><span style="color:#A020F0;">'EventName'</span>,<span style="color:#A020F0;">'ParamNames'</span>,<span style="color:#A020F0;">'EventData'</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
eventName  = eventData<span style="color: #080;">&#123;</span><span style="color: #33f;">1</span><span style="color: #080;">&#125;</span>;
paramNames = eventData<span style="color: #080;">&#123;</span><span style="color: #33f;">2</span><span style="color: #080;">&#125;</span>;
paramData  = eventData<span style="color: #080;">&#123;</span><span style="color: #33f;">3</span><span style="color: #080;">&#125;</span>.<span style="color: #0000FF;">cell</span>;</pre></div></div><p>In this innocent-looking code, <code>hEventData</code> is a Java object that contains the <b>EventName, ParamNames, EventData</b> properties: <b>EventName</b> is a Java <code>String</code>, that is automatically converted by Matlab&#8217;s <i><b>get</b>()</i> function into a Matlab string (<i><b>char</b></i> array); <b>ParamNames</b> is a Java array of <code>String</code>s, that gets automatically converted into a Matlab cell-array of string; and <b>EventData</b> is a Java array of <code>Object</code>s that needs to be converted into a Matlab cell array using the built-in <i><b>cell</b></i> function, as <a
target="_blank" href="http://undocumentedmatlab.com/blog/converting-java-vectors-to-matlab-arrays/">described</a> in one of my recent articles.</p><p>The code is indeed innocent, works really well and is actually extremely fast: each invocation takes of this code segment takes less than 0.2 millisecs. Unfortunately, because of the memory leak I needed to find a better alternative.</p><h3 id="handle">Using <i><b>handle</b>()</i></h3><p>The first idea was to use the built-in <i><b>handle</b>()</i> function, under the assumption that it would solve the memory leak, as <a
target="_blank" rel="nofollow" href="http://mathforum.org/kb/message.jspa?messageID=5950839">reported here</a>. In fact, MathWorks specifically advises to use <i><b>handle</b>()</i> rather than to work with &#8220;naked&#8221; Java objects, when <a
target="_blank" href="http://undocumentedmatlab.com/blog/uicontrol-callbacks/#memory_leak">setting Java object callbacks</a>. The official documentation of the <i><b>set</b></i> function <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/ref/set.html#f67-433534">says</a>:</p><blockquote><p>Do not use the set function on Java objects as it will cause a memory leak.</p></blockquote><p>It stands to reason then that a similar memory leak happens with <i><b>get</b></i> and that a similar use of <i><b>handle</b></i> would solve this problem:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% 300 uSecs per call, with memory leak</span>
s = <span style="color: #0000FF;">get</span><span style="color: #080;">&#40;</span>handle<span style="color: #080;">&#40;</span>hEventData<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
eventName  = s.<span style="">EventName</span>;
paramNames = s.<span style="">ParamNames</span>;
paramData  = <span style="color: #0000FF;">cell</span><span style="color: #080;">&#40;</span>s.<span style="">EventData</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Unfortunately, this variant, although working correctly, still leaks memory, and also performs almost twice as worse than the original version, taking some 0.3 milliseconds to execute per invocation. Looks like this is a dead end.</p><h3 id="accessor">Using Java accessor methods</h3><p>The next attempt was to use the Java object&#8217;s internal accessor methods for the requested properties. These are <code>public</code> methods of the form <i>getXXX(), isXXX(), setXXX()</i> that enable Matlab to treat XXX as a property by its <i><b>get</b></i> and <i><b>set</b></i> functions. In our case, we need to use the getter methods, as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% 380 uSecs per call, no memory leak</span>
eventName  = <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>hEventData.<span style="">getEventName</span><span style="color: #080;">&#41;</span>;
paramNames = <span style="color: #0000FF;">cell</span><span style="color: #080;">&#40;</span>hEventData.<span style="">getParamNames</span><span style="color: #080;">&#41;</span>;
paramData  = <span style="color: #0000FF;">cell</span><span style="color: #080;">&#40;</span>hEventData.<span style="">getEventData</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Here, the method <i>getEventName()</i> returns a Java <code>String</code>, that we convert into a Matlab string using the <i><b>char</b></i> function. In our previous two variants, the <i><b>get</b></i> function did this conversion for us automatically, but when we use the Java method directly we need to convert the results ourselves. Similarly, when we call <i>getParamNames()</i>, we need to use the <i><b>cell</b></i> function to convert the Java <code>String[]</code> array into a Matlab cell array.</p><p>This version at last doesn&#8217;t leak any memory. Unfortunately, it has an even worse performance: each invocation takes almost 0.4 milliseconds. The difference may seem insignificant. However, recall that this callback gets called dozens of times each second, so the total adds up quickly. It would be nice if there were a faster alternative that does not leak any memory.</p><h3 id="struct">Using <i><b>struct</b>()</i></h3><p>Luckily, I found just such an alternative. At 0.24 millisecs per invocation, it is almost as fast as the leaky best-performance original <i><b>get</b></i> version. Best of all, it leaks no memory, at least none that I could detect.</p><p>The mechanism relies on the little-known fact that public fields of Java objects can be retrieved in Matlab using the built-in <i><b>struct</b></i> function. For example:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; fields = <span style="color: #0000FF;">struct</span><span style="color: #080;">&#40;</span>java.<span style="">awt</span>.<span style="color: #0000FF;">Rectangle</span><span style="color: #080;">&#41;</span>
fields = 
             x<span style="color: #F0F;">:</span> <span style="color: #33f;">0</span>
             y<span style="color: #F0F;">:</span> <span style="color: #33f;">0</span>
         width<span style="color: #F0F;">:</span> <span style="color: #33f;">0</span>
        height<span style="color: #F0F;">:</span> <span style="color: #33f;">0</span>
      OUT_LEFT<span style="color: #F0F;">:</span> <span style="color: #33f;">1</span>
       OUT_TOP<span style="color: #F0F;">:</span> <span style="color: #33f;">2</span>
     OUT_RIGHT<span style="color: #F0F;">:</span> <span style="color: #33f;">4</span>
    OUT_BOTTOM<span style="color: #F0F;">:</span> <span style="color: #33f;">8</span>
&nbsp;
&gt;&gt; fields = <span style="color: #0000FF;">struct</span><span style="color: #080;">&#40;</span>java.<span style="">awt</span>.<span style="">Dimension</span><span style="color: #080;">&#41;</span>
fields = 
     width<span style="color: #F0F;">:</span> <span style="color: #33f;">0</span>
    height<span style="color: #F0F;">:</span> <span style="color: #33f;">0</span></pre></div></div><p>Note that this useful mechanism is not mentioned in <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/matlab_external/f4873.html#f46643">the main documentation page for accessing Java object fields</a>, although it is indeed mentioned in <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/matlab_external/f6671.html#f61403">another doc-page</a> &#8211; I guess this is a documentation oversight.</p><p>In any case, I converted my Java object to use public (rather than private) fields, so that I could use this <i><b>struct</b></i> mechanism (Matlab can only access public fields). Yes I know that using private fields is a better programming practice and all that (I&#8217;ve programmed OOP for some 15 years&#8230;), but sometimes we need to do ugly things in the interest of performance. The latest version now looks like this:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% 240 uSecs per call, no memory leak</span>
s = <span style="color: #0000FF;">struct</span><span style="color: #080;">&#40;</span>hEventData<span style="color: #080;">&#41;</span>;
eventName  = <span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>s.<span style="">eventName</span><span style="color: #080;">&#41;</span>;
paramNames = <span style="color: #0000FF;">cell</span><span style="color: #080;">&#40;</span>s.<span style="">paramNames</span><span style="color: #080;">&#41;</span>;
paramData  = <span style="color: #0000FF;">cell</span><span style="color: #080;">&#40;</span>s.<span style="">eventData</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>This solved the memory leakage issue for my client. I felt fortunate that I was not only able to detect Matlab&#8217;s memory leak but also find a working workaround without sacrificing performance or functionality.</p><p>In this particular case, I was lucky to have full control over my Java object, to be able to convert its fields to become public. Unfortunately, we do not always have similar control over the object that we use, because they were coded by a third party.</p><p>By the way, Matlab itself uses this <i><b>struct</b></i> mechanism in its code-base. For example, Matlab timers are implemented using Java objects (<code>com.mathworks.timer.TimerTask</code>). The timer callback in Matlab code converts the Java timer event data into a Matlab struct using the <i><b>struct</b></i> function, in <i>%matlabroot%/toolbox/matlab/iofun/@timer/timercb.m</i>. The users of the timer callbacks then get passed a simple Matlab EventData struct without ever knowing that the original data came from a Java object.</p><p>As an interesting corollary, this same <i><b>struct</b></i> mechanism can be used to detect internal properties of Matlab class objects. For example, in the timers again, we can get the underlying timer&#8217;s Java object as follows (note the highlighted warning, which I find a bit ironic given the context):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; timerObj = timerfind
&nbsp;
   Timer Object<span style="color: #F0F;">:</span> timer-<span style="color: #33f;">1</span>
&nbsp;
   Timer Settings
      ExecutionMode<span style="color: #F0F;">:</span> singleShot
             Period<span style="color: #F0F;">:</span> <span style="color: #33f;">1</span>
           BusyMode<span style="color: #F0F;">:</span> drop
            Running<span style="color: #F0F;">:</span> off
&nbsp;
   Callbacks
           TimerFcn<span style="color: #F0F;">:</span> @myTimerFcn
           ErrorFcn<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
           StartFcn<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
            StopFcn<span style="color: #F0F;">:</span> <span style="color:#A020F0;">''</span>
&nbsp;
&gt;&gt; timerFields = <span style="color: #0000FF;">struct</span><span style="color: #080;">&#40;</span>timerObj<span style="color: #080;">&#41;</span>
<span style="display:block;background-color: #ffc;"><span style="color: #0000FF;">Warning</span><span style="color: #F0F;">:</span> Calling <span style="color: #0000FF;">STRUCT</span> on an object prevents the object from hiding its implementation details and should thus be avoided.</span><span style="display:block;background-color: #ffc;"><span style="">Use</span> <span style="color: #0000FF;">DISP</span> or DISPLAY to see the visible public details of an object. <span style="">See</span> <span style="color:#A020F0;">'help struct'</span> <span style="color: #0000FF;">for</span> <span style="color: #0000FF;">more</span> information.</span><span style="display:block;background-color: #ffc;"><span style="color: #080;">&#40;</span><span style="color: #0000FF;">Type</span> &quot;warning off MATLAB<span style="color: #F0F;">:</span>structOnObject&quot; to suppress this <span style="color: #0000FF;">warning</span>.<span style="color: #080;">&#41;</span></span>timerFields = 
         ud<span style="color: #F0F;">:</span> <span style="color: #080;">&#123;</span><span style="color: #080;">&#125;</span>
    jobject<span style="color: #F0F;">:</span> <span style="color: #080;">&#91;</span>1x1 javahandle.<span style="">com</span>.<span style="">mathworks</span>.<span style="">timer</span>.<span style="">TimerTask</span><span style="color: #080;">&#93;</span></pre></div></div><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/profiling-matlab-memory-usage/' rel='bookmark' title='Profiling Matlab memory usage'>Profiling Matlab memory usage</a> <small>mtic and mtoc were a couple of undocumented features that enabled users of past Matlab releases to easily profile memory usage. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/preallocation-performance/' rel='bookmark' title='Preallocation performance'>Preallocation performance</a> <small>Preallocation is a standard Matlab speedup technique. Still, it has several undocumented aspects. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/performance-scatter-vs-line/' rel='bookmark' title='Performance: scatter vs. line'>Performance: scatter vs. line</a> <small>In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-java-interface-using-static-control/' rel='bookmark' title='Matlab-Java interface using a static control'>Matlab-Java interface using a static control</a> <small>The switchyard function design pattern can be very useful when setting Matlab callbacks to Java GUI controls. This article explains why and how....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/matlab-java-memory-leaks-performance/feed/</wfw:commentRss> <slash:comments>20</slash:comments> </item> <item><title>Converting Java vectors to Matlab arrays</title><link>http://undocumentedmatlab.com/blog/converting-java-vectors-to-matlab-arrays/</link> <comments>http://undocumentedmatlab.com/blog/converting-java-vectors-to-matlab-arrays/#comments</comments> <pubDate>Wed, 14 Dec 2011 18:00:47 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Java]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Performance]]></category> <category><![CDATA[Undocumented function]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2613</guid> <description><![CDATA[Converting Java vectors to Matlab arrays is pretty simple - this article explains how.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/matlab-java-memory-leaks-performance/' rel='bookmark' title='Matlab-Java memory leaks, performance'>Matlab-Java memory leaks, performance</a> <small>Internal fields of Java objects may leak memory - this article explains how to avoid this without sacrificing performance. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/' rel='bookmark' title='Matlab callbacks for Java events'>Matlab callbacks for Java events</a> <small>Events raised in Java code can be caught and handled in Matlab callback functions - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/java-stack-traces-in-matlab/' rel='bookmark' title='Java stack traces in Matlab'>Java stack traces in Matlab</a> <small>Matlab does not normally provide information about the Java calls on the stack trace. A simple trick can show us this information....</small></li><li><a
href='http://undocumentedmatlab.com/blog/jboost-integrating-an-external-java-library-in-matlab/' rel='bookmark' title='JBoost &#8211; Integrating an external Java library in Matlab'>JBoost &#8211; Integrating an external Java library in Matlab</a> <small>This article shows how an external Java library can be integrated in Matlab...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Matlab includes built-in support for automatic conversion of Matlab cell arrays into Java arrays. This is important in cases when we need to pass information to a Java function that expects an array (e.g., <code>String[]</code>).</p><h3 id="Numeric">Numeric data array</h3><p>In some cases, namely Java numeric arrays, Matlab also automatically converts the Java array into Matlab arrays. This is actually inconvenient when we would like to access the original Java reference in order to modify some value &#8211; since the Java reference is inaccessible from Matlab in this case, the data is immutable.</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; jColor = java.<span style="">awt</span>.<span style="">Color</span>.<span style="">red</span>
jColor =
java.<span style="">awt</span>.<span style="">Color</span><span style="color: #080;">&#91;</span>r=<span style="color: #33f;">255</span>,g=<span style="color: #33f;">0</span>,b=<span style="color: #33f;">0</span><span style="color: #080;">&#93;</span>
&nbsp;
&gt;&gt; matlabData = jColor.<span style="">getColorComponents</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
matlabData =
     <span style="color: #33f;">1</span>
     <span style="color: #33f;">0</span>     <span style="color: #228B22;">% &lt; = immutable array of numbers, not a reference to int[]</span>
     <span style="color: #33f;">0</span></pre></div></div><h3 id="Nonnumeric">Non-numeric array</h3><p>Very often we encounter cases in Java where the information is stored in an array of non-numeric data. In such cases we need to apply a non-automatic conversion from Java into Matlab.</p><p>If the objects are of exactly the same type, then we could store them in a simple Matlab array; otherwise (as can be seen in the example below), we could store them in either a simple array of <i><b>handle</b></i>s, or in a simple cell array:</p> </pre><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; jFrames = java.<span style="">awt</span>.<span style="">Frame</span>.<span style="">getFrames</span>
jFrames =
java.<span style="">awt</span>.<span style="">Frame</span><span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #F0F;">:</span>
    <span style="color: #080;">&#91;</span>javax.<span style="">swing</span>.<span style="">SwingUtilities</span>$SharedOwnerFrame <span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span>com.<span style="">mathworks</span>.<span style="">mde</span>.<span style="">desk</span>.<span style="">MLMainFrame</span>          <span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span>com.<span style="">mathworks</span>.<span style="">mde</span>.<span style="">desk</span>.<span style="">MLMultipleClientFrame</span><span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span>com.<span style="">mathworks</span>.<span style="">mwswing</span>.<span style="">MJFrame</span>               <span style="color: #080;">&#93;</span>
&nbsp;
<span style="color: #228B22;">% Alternative #1 - use a loop</span>
&gt;&gt; mFrames = handle<span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">for</span> idx = <span style="color: #33f;">1</span> <span style="color: #F0F;">:</span> <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>jFrames<span style="color: #080;">&#41;</span>; mFrames<span style="color: #080;">&#40;</span>idx<span style="color: #080;">&#41;</span>=handle<span style="color: #080;">&#40;</span>jFrames<span style="color: #080;">&#40;</span>idx<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">end</span>
&gt;&gt; mFrames
mFrames =
	handle<span style="color: #F0F;">:</span> <span style="color: #33f;">1</span>-by-<span style="color: #33f;">4</span>
&gt;&gt; mFrames<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> =
	javahandle.<span style="">javax</span>.<span style="">swing</span>.<span style="">SwingUtilities</span>$SharedOwnerFrame
&gt;&gt; mFrames<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> =
	javahandle.<span style="">com</span>.<span style="">mathworks</span>.<span style="">mde</span>.<span style="">desk</span>.<span style="">MLMainFrame</span>
&nbsp;
<span style="color: #228B22;">% Alternative #2a - convert into a Matlab cell array</span>
&gt;&gt; mFrames = jFrames.<span style="color: #0000FF;">cell</span>
mFrames = 
    <span style="color: #080;">&#91;</span>1x1 javax.<span style="">swing</span>.<span style="">SwingUtilities</span>$SharedOwnerFrame <span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span>1x1 com.<span style="">mathworks</span>.<span style="">mde</span>.<span style="">desk</span>.<span style="">MLMainFrame</span>          <span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span>1x1 com.<span style="">mathworks</span>.<span style="">mde</span>.<span style="">desk</span>.<span style="">MLMultipleClientFrame</span><span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span>1x1 com.<span style="">mathworks</span>.<span style="">mwswing</span>.<span style="">MJFrame</span>               <span style="color: #080;">&#93;</span>
&nbsp;
<span style="color: #228B22;">% Alternative #2b - convert to a cell array (equivalent variant of alternative 2a)</span>
&gt;&gt; mFrames = <span style="color: #0000FF;">cell</span><span style="color: #080;">&#40;</span>jFrames<span style="color: #080;">&#41;</span>;</pre></div></div><p>Note that if we only need to access a particular item in the Java vector or array, we could do that directly, without needing to convert the entire data into Matlab first. Simply use <code>jFrames(1)</code> to directly access the first item in the <code>jFrames</code> array, for example.</p><p>(note: Java Frames are discussed in chapters 7 and 8 of my Matlab-Java book).</p><h3 id="Collections">Vectors and other Collections</h3><p>Very often we encounter cases in Java where the information is stored in a <a
target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/tutorial/collections/index.html">Java Collection</a> rather than in a simple Java array. The basic mechanism for the conversion in this case is to first convert the Java data into a simple Java array (in cases it was not so in the first place), and then to convert this into a Matlab array using either the automated conversion (if the data is numeric), or using a for loop (ugly and slow!), or into a cell array using the <b><i>cell</i></b> function, as explained above.</p><p>Different Collections have different manners of converting into a Java array: some Collections return an Iterator/Enumerator that can be processed in a loop (be careful not to reset the iterator reference by re-reading it within the loop):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Wrong way - causes an infinite loop</span>
idx = <span style="color: #33f;">1</span>;
props = java.<span style="">lang</span>.<span style="">System</span>.<span style="">getProperties</span>;
<span style="color: #0000FF;">while</span> props.<span style="">elements</span>.<span style="">hasMoreElements</span>
    mPropValue<span style="color: #080;">&#123;</span>idx<span style="color: #080;">&#125;</span> = props.<span style="">elements</span>.<span style="">nextElement</span>;
<span style="color: #0000FF;">end</span>
&nbsp;
<span style="color: #228B22;">% Right way</span>
idx = <span style="color: #33f;">1</span>;
propValues = java.<span style="">lang</span>.<span style="">System</span>.<span style="">getProperties</span>.<span style="">elements</span>;  <span style="color: #228B22;">% Enumerator</span>
<span style="color: #0000FF;">while</span> propValues.<span style="">hasMoreElements</span>
    mPropValue<span style="color: #080;">&#123;</span>idx<span style="color: #080;">&#125;</span> = propValues.<span style="">nextElement</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p>(note: system properties are discussed in section 1.9 of my Matlab-Java book; Collections are discussed in section 2.1)</p><p>Other Collections, such as <a
target="_blank" rel="nofollow" href="http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Vector.html"><code>java.util.Vector</code></a>, have a <i>toArray()</i> method that directly converts into a Java array, and we can process from there as described above:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; jVector = java.<span style="">util</span>.<span style="">Vector</span>;
&gt;&gt; jVector.<span style="">add</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>; jVector.<span style="">add</span><span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>; jVector.<span style="">add</span><span style="color: #080;">&#40;</span><span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>;
&gt;&gt; jVector.<span style="">addAll</span><span style="color: #080;">&#40;</span>jv<span style="color: #080;">&#41;</span>; jVector.<span style="">addAll</span><span style="color: #080;">&#40;</span>jv<span style="color: #080;">&#41;</span>;
&gt;&gt; jVector
jVector =
<span style="color: #080;">&#91;</span><span style="color: #33f;">1.0</span>, <span style="color: #33f;">2.0</span>, <span style="color: #33f;">3.0</span>, <span style="color: #33f;">1.0</span>, <span style="color: #33f;">2.0</span>, <span style="color: #33f;">3.0</span>, <span style="color: #33f;">1.0</span>, <span style="color: #33f;">2.0</span>, <span style="color: #33f;">3.0</span>, <span style="color: #33f;">1.0</span>, <span style="color: #33f;">2.0</span>, <span style="color: #33f;">3.0</span><span style="color: #080;">&#93;</span>
&nbsp;
<span style="color: #228B22;">% Now convert into a Matlab cell array via a Java simple array</span>
&gt;&gt; mCellArray = jVector.<span style="">toArray</span>.<span style="color: #0000FF;">cell</span>
mCellArray = 
    <span style="color: #080;">&#91;</span><span style="color: #33f;">1</span><span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span><span style="color: #33f;">2</span><span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span><span style="color: #33f;">3</span><span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span><span style="color: #33f;">1</span><span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span><span style="color: #33f;">2</span><span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span><span style="color: #33f;">3</span><span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span><span style="color: #33f;">1</span><span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span><span style="color: #33f;">2</span><span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span><span style="color: #33f;">3</span><span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span><span style="color: #33f;">1</span><span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span><span style="color: #33f;">2</span><span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span><span style="color: #33f;">3</span><span style="color: #080;">&#93;</span></pre></div></div><h3 id="Performance">Performance</h3><p>It so happens, that the undocumented built-in <a
target="_blank" href="http://undocumentedmatlab.com/blog/undocumented-feature-function/"><i><b>feature</b></i> function</a> (or its near-synonym <i><b>system_dependent</b></i>) enables improved performance in this conversion process. <i><b>feature</b></i>(44) accepts a <code>java.util.Vector</code> and converts it directly into a Matlab cell-array, in one third to one-half the time that it would take the equivalent <i>toArray.cell()</i> (the third input argument is the number of columns in the result - the reshaping is done automatically):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; mCellArray = feature<span style="color: #080;">&#40;</span><span style="color: #33f;">44</span>,jVector,jVector.<span style="color: #0000FF;">size</span><span style="color: #080;">&#41;</span>   <span style="color: #228B22;">% jVector.size = 12</span>
mCellArray = 
    <span style="color: #080;">&#91;</span><span style="color: #33f;">1</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">2</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">3</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">1</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">2</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">3</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">1</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">2</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">3</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">1</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">2</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">3</span><span style="color: #080;">&#93;</span>
&nbsp;
&gt;&gt; mCellArray = feature<span style="color: #080;">&#40;</span><span style="color: #33f;">44</span>,jVector,<span style="color: #33f;">4</span><span style="color: #080;">&#41;</span>
mCellArray = 
    <span style="color: #080;">&#91;</span><span style="color: #33f;">1</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">1</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">1</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">1</span><span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span><span style="color: #33f;">2</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">2</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">2</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">2</span><span style="color: #080;">&#93;</span>
    <span style="color: #080;">&#91;</span><span style="color: #33f;">3</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">3</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">3</span><span style="color: #080;">&#93;</span>    <span style="color: #080;">&#91;</span><span style="color: #33f;">3</span><span style="color: #080;">&#93;</span></pre></div></div><p>The conversion process is pretty efficient: On my system, the regular <i>toArray.cell()</i> takes 0.45 seconds for a 100K vector, compared to 0.21 seconds for the <i><b>feature</b></i> alternative. However, this small difference could be important in cases where performance is crucial, for example in <a
target="_blank" href="http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/">processing of highly-active Java events in Matlab callbacks</a>, or when retrieving data from a database. And this latter case is indeed where a sample usage of this <i><b>feature</b></i> can be found, namely in the <i><b>cursor.fetch.m</b></i> function (where it appears as <i><b>system_dependent(44)</b></i>).</p><p>Please note that both <i><b>feature</b></i> and <i><b>system_dependent</b></i> are highly prone to change without prior warning in some future Matlab release. On the other hand, the conversion methods that I presented above, excluding <i><b>feature</b></i>, will probably still be valid in all Matlab releases in the near future.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/matlab-java-memory-leaks-performance/' rel='bookmark' title='Matlab-Java memory leaks, performance'>Matlab-Java memory leaks, performance</a> <small>Internal fields of Java objects may leak memory - this article explains how to avoid this without sacrificing performance. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/' rel='bookmark' title='Matlab callbacks for Java events'>Matlab callbacks for Java events</a> <small>Events raised in Java code can be caught and handled in Matlab callback functions - this article explains how...</small></li><li><a
href='http://undocumentedmatlab.com/blog/java-stack-traces-in-matlab/' rel='bookmark' title='Java stack traces in Matlab'>Java stack traces in Matlab</a> <small>Matlab does not normally provide information about the Java calls on the stack trace. A simple trick can show us this information....</small></li><li><a
href='http://undocumentedmatlab.com/blog/jboost-integrating-an-external-java-library-in-matlab/' rel='bookmark' title='JBoost &#8211; Integrating an external Java library in Matlab'>JBoost &#8211; Integrating an external Java library in Matlab</a> <small>This article shows how an external Java library can be integrated in Matlab...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/converting-java-vectors-to-matlab-arrays/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>datestr performance</title><link>http://undocumentedmatlab.com/blog/datestr-performance/</link> <comments>http://undocumentedmatlab.com/blog/datestr-performance/#comments</comments> <pubDate>Wed, 05 Oct 2011 20:17:28 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Performance]]></category> <category><![CDATA[Pure Matlab]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2453</guid> <description><![CDATA[Caching is a simple and very effective means to improve code performance, as demonstrated for the datestr function.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/performance-scatter-vs-line/' rel='bookmark' title='Performance: scatter vs. line'>Performance: scatter vs. line</a> <small>In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster...</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-performance/' rel='bookmark' title='Plot performance'>Plot performance</a> <small>Undocumented inner plot mechanisms can be used to significantly improved plotting performance...</small></li><li><a
href='http://undocumentedmatlab.com/blog/datenum-performance/' rel='bookmark' title='Datenum performance'>Datenum performance</a> <small>The performance of the built-in Matlab function datenum can be significantly improved by using an undocumented internal help function...</small></li><li><a
href='http://undocumentedmatlab.com/blog/preallocation-performance/' rel='bookmark' title='Preallocation performance'>Preallocation performance</a> <small>Preallocation is a standard Matlab speedup technique. Still, it has several undocumented aspects. ...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>A few months ago, I posted an <a
target="_blank" href="http://undocumentedmatlab.com/blog/datenum-performance/">article</a> showing how we can use some internal help functions to significantly improve the performance of the commonly-used <i><b>datenum</b></i> function. The catch is that we must be certain of certain preconditions before we can use this method, otherwise we might get incorrect results.</p><p>Today I wish to share a related experience that happened to me yesterday, when I needed to improve the performance of a client&#8217;s application. When profiling the application, I found that a major performance hotspot was a repeated call to the <i><b>datestr</b></i> function, each time with several thousand date items.</p><p><i><b>datestr</b></i> is the opposite function of <i><b>datenum</b></i>: it receives date values and returns date strings. Unlike <i><b>datenum</b></i>, however, <i><b>datestr</b></i> does not use a highly optimized native-code library function that we could use directly. Instead, it loops over all date values and sequentially applies the requested string pattern.</p><p>The natural reaction in such a case would perhaps be to vectorize the code (something that MathWorks should have done in the first place I guess). But in this case I used a different solution, that I would like to share today:</p><p>In any programming languages, Matlab included, the most effective performance tip is to cache processing results. Caching often makes the code slightly more complex and less maintainable, but the performance benefits are immediate and significant. In Matlab, benefits of caching can often surpass even those of vectorization (using both vectorization <u>and</u> caching is of course even better).</p><p>In the case of <i><b>datestr</b></i>, if we can be certain of the precondition that the output string format is the same, we can cache the results, and even use vectorization. In my case, I plotted historical daily stock quotes data and so I was assured that (1) all dates are integers and that (2) I always use the same date-string format &#8216;dd-mmm-yyyy&#8217;.</p><p>First, let&#8217;s define the wrapper function <i><b>datestr2</b></i> with the necessary caching and vectorization:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% datestr2 - faster variant of datestr, for integer date values since 1/1/2000</span>
<span style="color: #0000FF;">function</span> dateStrs = datestr2<span style="color: #080;">&#40;</span>dateVals,<span style="color: #0000FF;">varargin</span><span style="color: #080;">&#41;</span>
&nbsp;
  <span style="color: #0000FF;">persistent</span> dateStrsCache
  <span style="color: #0000FF;">persistent</span> dateValsCache
&nbsp;
  <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">isempty</span><span style="color: #080;">&#40;</span>dateStrsCache<span style="color: #080;">&#41;</span>
      origin = <span style="color: #0000FF;">datenum</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'1-Jan-2000'</span><span style="color: #080;">&#41;</span>;
      dateValsCache = origin<span style="color: #F0F;">:</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">now</span>+<span style="color: #33f;">100</span><span style="color: #080;">&#41;</span>;
      dateStrsCache = <span style="color: #0000FF;">datestr</span><span style="color: #080;">&#40;</span>dateValsCache,<span style="color: #0000FF;">varargin</span><span style="color: #080;">&#123;</span><span style="color: #F0F;">:</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">end</span>
&nbsp;
  <span style="color: #080;">&#91;</span>tf,loc<span style="color: #080;">&#93;</span> = <span style="color: #0000FF;">ismember</span><span style="color: #080;">&#40;</span>dateVals, dateValsCache<span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">if</span> <span style="color: #0000FF;">all</span><span style="color: #080;">&#40;</span>tf<span style="color: #080;">&#41;</span>
      dateStrs = dateStrsCache<span style="color: #080;">&#40;</span>loc,<span style="color: #F0F;">:</span><span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">else</span>
      dateStrs = <span style="color: #0000FF;">datestr</span><span style="color: #080;">&#40;</span>dateVals,<span style="color: #0000FF;">varargin</span><span style="color: #080;">&#123;</span><span style="color: #F0F;">:</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>;
  <span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">end</span>  <span style="color: #228B22;">% datestr2</span></pre></div></div><p>As can be seen, the first time that <i><b>datestr2</b></i> is called, it computes and caches all <i><b>datestr</b></i> values for all the dates since Jan 1, 2000. Subsequent calls to <i><b>datestr2</b></i> simply retrieve the relevant cache values. Note that the input date entries need not be sorted.</p><p>In case that an input date number is not found in the cache, <i><b>datestr2</b></i> automatically falls-back to using the built-in <i><b>datestr</b></i> for the entire input list. This could of course be improved to add the new entries to the cache &#8211; I leave this as a reader exercise.</p><p>The bottom line was a <b>150-times (!!!) speed improvement</b> for a 1000-item date vector (50mS => 0.3mS on my system):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Prepare a 1000-vector of dates, starting 3 years ago until today</span>
&gt;&gt; dateVals = <span style="color: #0000FF;">fix</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">now</span><span style="color: #080;">&#41;</span>+<span style="color: #080;">&#40;</span>-<span style="color: #33f;">1000</span><span style="color: #F0F;">:</span><span style="color: #33f;">0</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Run the standard datestr function =&gt; 50mS</span>
&gt;&gt; <span style="color: #0000FF;">tic</span>; s1=<span style="color: #0000FF;">datestr</span><span style="color: #080;">&#40;</span>dateVals<span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.049089</span> seconds.
&gt;&gt; <span style="color: #0000FF;">tic</span>; s1=<span style="color: #0000FF;">datestr</span><span style="color: #080;">&#40;</span>dateVals<span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.048086</span> seconds.
&nbsp;
<span style="color: #228B22;">% Now run our datestr2 function (caching already done before) =&gt; 0.3 mS</span>
&gt;&gt; <span style="color: #0000FF;">tic</span>; s2=datestr2<span style="color: #080;">&#40;</span>dateVals<span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.222031</span> seconds.   <span style="color: #228B22;">% initial cache preparation takes 222 mS</span>
&gt;&gt; <span style="color: #0000FF;">tic</span>; s2=datestr2<span style="color: #080;">&#40;</span>dateVals<span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.000313</span> seconds.   <span style="color: #228B22;">% subsequent datestr2 calls take 0.3 mS</span>
&gt;&gt; <span style="color: #0000FF;">tic</span>; s2=datestr2<span style="color: #080;">&#40;</span>dateVals<span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.000296</span> seconds.
&nbsp;
<span style="color: #228B22;">% Ensure that the two functions give exactly the same results</span>
&gt;&gt; isequal<span style="color: #080;">&#40;</span>s1,s2<span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> =
     <span style="color: #33f;">1</span></pre></div></div><p>So what have we learned from this?</p><ol><li>To improve performance we must profile the code. Very often the performance bottlenecks occur in non-intuitive very specific places that can be surgically handled without requiring any major redesign. In this case, I simply had to replace calls to <i><b>datestr</b></i> with <i><b>datestr2</b></i> in the application&#8217;s code.</li><li>Vectorization is not always as cost-effective as caching</li><li>Major performance improvements do NOT necessarily involve undocumented functions or tricks: In fact, today&#8217;s post about caching uses fully-documented pure-Matlab code.</li><li>Different performance hotspots can have different solutions: caching, vectorization, <a
target="_blank" href="http://undocumentedmatlab.com/blog/datenum-performance/">internal library functions</a>, <a
target="_blank" href="http://undocumentedmatlab.com/blog/plot-liminclude-properties/">undocumented graphics properties</a>, <a
target="_blank" href="http://undocumentedmatlab.com/blog/plot-performance/">smart property selection</a>, <a
target="_blank" href="http://undocumentedmatlab.com/blog/performance-scatter-vs-line/">smart function selection</a>, <a
target="_blank" href="http://undocumentedmatlab.com/blog/matrix-processing-performance/">smart indexing</a>, <a
target="_blank" href="http://undocumentedmatlab.com/blog/cellfun-undocumented-performance-boost/">smart parameter selection</a> etc.</li></ol><p>In a later post I will show how similar modifications to internal Matlab functions can dramatically improve the performance of the <i><b>uitable</b></i> function. Anyone who has tried using <i><b>uitable</b></i> with more than a few dozen cells will surely understand why this is important&#8230;</p><p>Do you have a favorite performance trick not mentioned above? If so, please post a <a
href="http://undocumentedmatlab.com/blog/datestr-performance/#respond">comment</a>.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/performance-scatter-vs-line/' rel='bookmark' title='Performance: scatter vs. line'>Performance: scatter vs. line</a> <small>In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster...</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-performance/' rel='bookmark' title='Plot performance'>Plot performance</a> <small>Undocumented inner plot mechanisms can be used to significantly improved plotting performance...</small></li><li><a
href='http://undocumentedmatlab.com/blog/datenum-performance/' rel='bookmark' title='Datenum performance'>Datenum performance</a> <small>The performance of the built-in Matlab function datenum can be significantly improved by using an undocumented internal help function...</small></li><li><a
href='http://undocumentedmatlab.com/blog/preallocation-performance/' rel='bookmark' title='Preallocation performance'>Preallocation performance</a> <small>Preallocation is a standard Matlab speedup technique. Still, it has several undocumented aspects. ...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/datestr-performance/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> <item><title>Matrix processing performance</title><link>http://undocumentedmatlab.com/blog/matrix-processing-performance/</link> <comments>http://undocumentedmatlab.com/blog/matrix-processing-performance/#comments</comments> <pubDate>Wed, 13 Jul 2011 16:57:09 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Performance]]></category> <category><![CDATA[Pure Matlab]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2380</guid> <description><![CDATA[Matrix operations performance is affected by internal subscriptions in a counter-intuitive way.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/preallocation-performance/' rel='bookmark' title='Preallocation performance'>Preallocation performance</a> <small>Preallocation is a standard Matlab speedup technique. Still, it has several undocumented aspects. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/performance-scatter-vs-line/' rel='bookmark' title='Performance: scatter vs. line'>Performance: scatter vs. line</a> <small>In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster...</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-performance/' rel='bookmark' title='Plot performance'>Plot performance</a> <small>Undocumented inner plot mechanisms can be used to significantly improved plotting performance...</small></li><li><a
href='http://undocumentedmatlab.com/blog/datestr-performance/' rel='bookmark' title='datestr performance'>datestr performance</a> <small>Caching is a simple and very effective means to improve code performance, as demonstrated for the datestr function....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>A few days ago, fellow Matlab blogger Roy Fahn, well-respected in the Israeli Matlab community, posted an <a
target="_blank" rel="nofollow" href="http://matlabisrael.blogspot.com/2011/07/blog-post.html">interesting article</a> on his <a
target="_blank" rel="nofollow" href="http://matlabisrael.blogspot.com/">MATLAB with Fun</a> blog (note the word-play). Since his article is in Hebrew, and the <a
target="_blank" rel="nofollow" href="http://translate.google.com/translate?hl=en&#038;sl=iw&#038;tl=en&#038;u=http%3A%2F%2Fmatlabisrael.blogspot.com%2F2011%2F07%2Fblog-post.html">automated Google Translation</a> is somewhat lacking, I thought of sharing Roy&#8217;s post here (with his permission of course), for the minority of the Matlab community which is not fluent in Hebrew&#8230;</p><h3 id="translation">Roy&#8217;s translated post: <i>&#8220;Anyone who adds, detracts (from execution time)&#8221;</i></h3><p>In the story of Eve and the serpent, the first woman told the serpent about the prohibition of <i>eating</i> from the Tree of Knowledge, adding to that prohibition a ban on <i>touching</i> the tree (something that God has not commanded). The snake used this inaccuracy in her words, showing her that one can touch the tree without fear, and therefore argued that the prohibition to eat its fruit is similarly not true. As a result, Eve was tempted to eat the fruit, and the rest is known. Jewish sages said of the imaginary prohibition which Eve has added, that this is an example where &#8220;Anyone who adds, [in effect] detracts&#8221;.</p><p>Recently I [Roy] came across an interesting phenomenon, that in MATLAB, adding elements to a vector on which an action is performed, does not degrade the execution time, but rather the reverse. Adding vector elements actually reduces execution time!</p><p>Here&#8217;s an example. Try to rank the following tic-toc segments from fastest to slowest:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">x = <span style="color: #0000FF;">rand</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1000</span>,<span style="color: #33f;">1000</span><span style="color: #080;">&#41;</span>;
&nbsp;
<span style="color: #228B22;">% Segment #1</span>
y = <span style="color: #0000FF;">ones</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1000</span>,<span style="color: #33f;">1000</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">tic</span>
<span style="color: #0000FF;">for</span> <span style="color: #0000FF;"><span style="color: #33f;">i</span></span>=<span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">100</span>
    y = x .* y;
<span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">toc</span>
&nbsp;
<span style="color: #228B22;">% Segment #2</span>
y=<span style="color: #0000FF;">ones</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1000</span>,<span style="color: #33f;">1000</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">tic</span>
<span style="color: #0000FF;">for</span> <span style="color: #0000FF;"><span style="color: #33f;">i</span></span>=<span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">100</span>
    y<span style="color: #080;">&#40;</span><span style="color: #F0F;">:</span>,<span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">999</span><span style="color: #080;">&#41;</span> = x<span style="color: #080;">&#40;</span><span style="color: #F0F;">:</span>,<span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">999</span><span style="color: #080;">&#41;</span> .* y<span style="color: #080;">&#40;</span><span style="color: #F0F;">:</span>,<span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">999</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">toc</span>
&nbsp;
<span style="color: #228B22;">% Segment #3</span>
y=<span style="color: #0000FF;">ones</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1000</span>,<span style="color: #33f;">1000</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">tic</span>
<span style="color: #0000FF;">for</span> <span style="color: #0000FF;"><span style="color: #33f;">i</span></span>=<span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">100</span>
    y<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">999</span>,<span style="color: #F0F;">:</span><span style="color: #080;">&#41;</span> = x<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">999</span>,<span style="color: #F0F;">:</span><span style="color: #080;">&#41;</span> .* y<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">999</span>,<span style="color: #F0F;">:</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span>
<span style="color: #0000FF;">toc</span></pre></div></div><p>The first loop multiplies all the elements of the x and y matrices, and should therefore run longer than the other loops, which multiply matrices that are one row or one column smaller. However, in practice, the first loop was the fastest &#8211; just 0.25 seconds on my computer, whereas the second ran for 1.75 seconds, and the third &#8211; 6.65 seconds [YMMV].</p><p>Why is the first loop the fastest?</p><p>The subscription operation performed in each of the latter two loops is a wasteful action, and therefore in such cases I would suggest that you run your operation on the full matrix, and then get rid of the unnecessary row or column.</p><p>And why does the second loop run faster than the third?</p><p>This is related to the fact that MATLAB prefers operations on columns rather than rows. In the second loop, all the elements are multiplied except those in the last column, while in the third loop all the elements that have been extracted from all rows are multiplied, except for the last row.</p><p>In your work with MATLAB, have you encountered similar phenomena that are initially counter-intuitive, such as the example described above? If so, please post a comment <a
href="http://undocumentedmatlab.com/blog/array-processing-performance/#respond">below</a>, or <a
target="_blank" rel="nofollow" href="http://matlabisrael.blogspot.com/2011/07/blog-post.html#comment-form">directly on Roy&#8217;s blog</a>.</p><p>Is all of this undocumented? I really don&#8217;t know. But it is certainly unexpected and interesting&#8230;</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/preallocation-performance/' rel='bookmark' title='Preallocation performance'>Preallocation performance</a> <small>Preallocation is a standard Matlab speedup technique. Still, it has several undocumented aspects. ...</small></li><li><a
href='http://undocumentedmatlab.com/blog/performance-scatter-vs-line/' rel='bookmark' title='Performance: scatter vs. line'>Performance: scatter vs. line</a> <small>In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster...</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-performance/' rel='bookmark' title='Plot performance'>Plot performance</a> <small>Undocumented inner plot mechanisms can be used to significantly improved plotting performance...</small></li><li><a
href='http://undocumentedmatlab.com/blog/datestr-performance/' rel='bookmark' title='datestr performance'>datestr performance</a> <small>Caching is a simple and very effective means to improve code performance, as demonstrated for the datestr function....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/matrix-processing-performance/feed/</wfw:commentRss> <slash:comments>9</slash:comments> </item> <item><title>More undocumented timing features</title><link>http://undocumentedmatlab.com/blog/more-undocumented-timing-features/</link> <comments>http://undocumentedmatlab.com/blog/more-undocumented-timing-features/#comments</comments> <pubDate>Wed, 06 Jul 2011 18:00:33 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Performance]]></category> <category><![CDATA[Pure Matlab]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2365</guid> <description><![CDATA[There are several undocumented ways in Matlab to get CPU and clock data<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/tic-toc-undocumented-option/' rel='bookmark' title='tic / toc &#8211; undocumented option'>tic / toc &#8211; undocumented option</a> <small>Matlab's built-in tic/toc functions have an undocumented option enabling multiple nested clockings...</small></li><li><a
href='http://undocumentedmatlab.com/blog/undocumented-feature-function/' rel='bookmark' title='Undocumented feature() function'>Undocumented feature() function</a> <small>Matlab's undocumented feature function enables access to some internal experimental features...</small></li><li><a
href='http://undocumentedmatlab.com/blog/undocumented-scatter-plot-behavior/' rel='bookmark' title='Undocumented scatter plot behavior'>Undocumented scatter plot behavior</a> <small>The scatter plot function has an undocumented behavior when plotting more than 100 points: it returns a single unified patch object handle, rather than a patch handle for each specific...</small></li><li><a
href='http://undocumentedmatlab.com/blog/types-of-undocumented-matlab-aspects/' rel='bookmark' title='Types of undocumented Matlab aspects'>Types of undocumented Matlab aspects</a> <small>This article lists the different types of undocumented/unsupported/hidden aspects in Matlab...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Two years ago, I <a
target="_blank" href="http://undocumentedmatlab.com/blog/tic-toc-undocumented-option/">posted</a> about a previously-undocumented feature of the built-in <i><b>tic/toc</b></i> functions that enables using them in nested operations. Apparently, <code>tStart=tic</code> returns a <i><b>uint64</b></i> value that represents the number of cpu cycles since the last computer restart (as several people have <a
target="_blank" href="http://undocumentedmatlab.com/blog/tic-toc-undocumented-option/#comments">commented</a> in that post).</p><p>Since that time I have found a few additional related titbits that I would like to share:</p><h3 id="cpucount">cpucount</h3><p>It appears that the CPU count value returned by <i><b>tic</b></i> is also returned by the internal executable application <code>cpucount</code> (or <code>cpucount.exe</code>), which is located beneath the <code>bin</code> folder of the Matlab installation (for example: <code>C:\Program Files\Matlab\R2011a\bin\win32\cpucount.exe</code> or <code>/bin/matlab/R2011a/bin/glnx86/cpucount</code>):</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; !cpucount
<span style="color: #33f;">7144479469070</span> 
&nbsp;
&gt;&gt; uint64<span style="color: #080;">&#40;</span><span style="color: #0000FF;">str2num</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">evalc</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'!cpucount'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> =
        <span style="color: #33f;">7144486156548</span>
&nbsp;
&gt;&gt; tStart = <span style="color: #0000FF;">tic</span>
tStart =
        <span style="color: #33f;">7144497276916</span>
&nbsp;
&gt;&gt; uint64<span style="color: #080;">&#40;</span><span style="color: #0000FF;">str2num</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">evalc</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'!cpucount'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> - <span style="color: #0000FF;">tic</span>
<span style="color: #0000FF;">ans</span> =
                    <span style="color: #33f;">0</span></pre></div></div><p>Note that the <code>cpucount</code> application should not be confused with the built-in <i><b>cputime</b></i> function, which returns the total CPU time (in seconds) used by the current Matlab process.</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; <span style="color: #0000FF;">cputime</span>
<span style="color: #0000FF;">ans</span> =
                 <span style="color: #33f;">210.15625</span></pre></div></div><p>Note that the CPU count value itself should typically not be used to profile performance, but rather as a unique seed for random numbers and for multiple independent <i><b>tic/toc</b></i> invocations. This reminds me of a discussion that arose a decade ago, about the <i><b>flops</b></i> function&#8217;s <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/company/newsletters/news_notes/clevescorner/winter2000.cleve.html">removal in Matlab 6.0</a>, when LAPACK was introduced. There are far better ways today for code <a
target="_blank" href="http://undocumentedmatlab.com/blog/undocumented-profiler-options/">profiling</a>.</p><p>Anyway, the value reported by both <code>cpucount</code> and <i><b>tic</b></i>, is also returned by <code>feature('timing','winperfcount')</code>. Which brings us to:</p><h3 id="feature">feature(&#8216;timing&#8217;)</h3><p>A related tidbit is the &#8216;timing&#8217; option of the built-in undocumented <i><b>feature</b></i> function, which I <a
target="_blank" href="http://undocumentedmatlab.com/blog/undocumented-feature-function/">explored</a> last year:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; feature<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'timing'</span><span style="color: #080;">&#41;</span>
??? <span style="color: #0000FF;">Error</span> using ==&gt; feature
Choose second argument from<span style="color: #F0F;">:</span>
    <span style="color:#A020F0;">'resolution_tictoc'</span>  - Resolution of <span style="color: #0000FF;">Tic</span>/<span style="color: #0000FF;">Toc</span> <span style="color: #0000FF;">clock</span> in <span style="color: #0000FF;">sec</span> <span style="color: #080;">&#40;</span><span style="color: #0000FF;">double</span><span style="color: #080;">&#41;</span>
    <span style="color:#A020F0;">'overhead_tictoc'</span>    - Overhead of <span style="color: #0000FF;">Tic</span>/<span style="color: #0000FF;">Toc</span> command in <span style="color: #0000FF;">sec</span> <span style="color: #080;">&#40;</span><span style="color: #0000FF;">double</span><span style="color: #080;">&#41;</span>
    <span style="color:#A020F0;">'cpucount'</span>           - Current CPU cycles used <span style="color: #080;">&#40;</span>uint64<span style="color: #080;">&#41;</span> <span style="color: #080;">&#91;</span>Using utCPUcount<span style="color: #080;">&#93;</span>
    <span style="color:#A020F0;">'getcpuspeed_tictoc'</span> - Stored CPU cycles/<span style="color: #0000FF;">sec</span> <span style="color: #080;">&#40;</span><span style="color: #0000FF;">double</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&#91;</span>Used by <span style="color: #0000FF;">tic</span>/<span style="color: #0000FF;">toc</span><span style="color: #080;">&#93;</span>
    <span style="color:#A020F0;">'cpuspeed'</span>           - Current CPU cycles/<span style="color: #0000FF;">sec</span> <span style="color: #080;">&#40;</span><span style="color: #0000FF;">double</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&#91;</span>Simple MathWorks<span style="color: #080;">&#93;</span>
    <span style="color:#A020F0;">'winperfcount'</span>       - Current CPU cycles used <span style="color: #080;">&#40;</span>uint64<span style="color: #080;">&#41;</span> <span style="color: #080;">&#91;</span>Windows call<span style="color: #080;">&#93;</span>
    <span style="color:#A020F0;">'winperfspeed'</span>       - Current CPU cycles/<span style="color: #0000FF;">sec</span> <span style="color: #080;">&#40;</span><span style="color: #0000FF;">double</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&#91;</span>Windows call<span style="color: #080;">&#93;</span>
    <span style="color:#A020F0;">'wintime'</span>            - Current Windows time <span style="color: #080;">&#40;</span><span style="color: #0000FF;">uint32</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&#91;</span>Windows call<span style="color: #080;">&#93;</span>
                           units<span style="color: #F0F;">:</span> msec since startup <span style="color: #080;">&#91;</span>Wraps<span style="color: #080;">&#93;</span>
    <span style="color:#A020F0;">'wintimeofday'</span>       - Current time of day converted to file time <span style="color: #080;">&#40;</span>unit64<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span>Windows call<span style="color: #080;">&#93;</span>
                           units<span style="color: #F0F;">:</span> <span style="color: #33f;">100</span> nsec since 01-Jan-<span style="color: #33f;">1601</span> <span style="color: #080;">&#40;</span>UTC<span style="color: #080;">&#41;</span>
    <span style="color:#A020F0;">'clocks_per_sec'</span>     - <span style="color: #0000FF;">clock</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> speed in cycles/<span style="color: #0000FF;">sec</span> <span style="color: #080;">&#91;</span>CLOCKS_PER_SEC<span style="color: #080;">&#93;</span>
Choose second and third arguments from<span style="color: #F0F;">:</span>
    <span style="color:#A020F0;">'cpuspeed'</span>, <span style="color: #0000FF;">double</span> num             - Current CPU cycles/<span style="color: #0000FF;">sec</span> <span style="color: #080;">&#40;</span><span style="color: #0000FF;">double</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&#91;</span>MathWorks - num iterations<span style="color: #080;">&#93;</span>
    <span style="color:#A020F0;">'setcpuspeed_tictoc'</span>, <span style="color: #0000FF;">double</span> speed - <span style="color: #0000FF;">Set</span> the CPU cycles/<span style="color: #0000FF;">sec</span> <span style="color: #080;">&#91;</span>Used by <span style="color: #0000FF;">tic</span>/<span style="color: #0000FF;">toc</span><span style="color: #080;">&#93;</span>
     uint64 arg2, uint64 arg3          - uint64 difference = arg2 - arg3 <span style="color: #080;">&#40;</span>uint64<span style="color: #080;">&#41;</span>
&nbsp;
&gt;&gt; feature<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'timing'</span>,<span style="color:#A020F0;">'resolution_tictoc'</span><span style="color: #080;">&#41;</span>
Resolution of <span style="color: #0000FF;">Tic</span>/<span style="color: #0000FF;">Toc</span> <span style="color: #0000FF;">clock</span> <span style="color: #0000FF;">is</span> 1.676191e-006 <span style="color: #0000FF;">sec</span>.
&nbsp;
&gt;&gt; feature<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'timing'</span>,<span style="color:#A020F0;">'overhead_tictoc'</span><span style="color: #080;">&#41;</span>
Overhead of <span style="color: #0000FF;">Tic</span>/<span style="color: #0000FF;">Toc</span> command <span style="color: #0000FF;">is</span> 1.676191e-006 <span style="color: #0000FF;">sec</span>.
&nbsp;
&gt;&gt; feature<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'timing'</span>,<span style="color:#A020F0;">'cpucount'</span><span style="color: #080;">&#41;</span>
CPU counter <span style="color: #0000FF;">is</span> <span style="color: #33f;">17548354355882</span> cycles.
&nbsp;
&gt;&gt; feature<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'timing'</span>,<span style="color:#A020F0;">'getcpuspeed_tictoc'</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">tic</span>/<span style="color: #0000FF;">toc</span> CPU speed <span style="color: #0000FF;">is</span> 2.533333e+009 cycles/<span style="color: #0000FF;">sec</span>.
&nbsp;
&gt;&gt; feature<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'timing'</span>,<span style="color:#A020F0;">'cpuspeed'</span><span style="color: #080;">&#41;</span>
Current CPU speed <span style="color: #0000FF;">is</span> 2.535690e+009 cycles/<span style="color: #0000FF;">sec</span>.
&nbsp;
&gt;&gt; feature<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'timing'</span>,<span style="color:#A020F0;">'winperfcount'</span><span style="color: #080;">&#41;</span>
Counter <span style="color: #0000FF;">is</span> <span style="color: #33f;">7150537513228</span> cycles.
&nbsp;
&gt;&gt; feature<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'timing'</span>,<span style="color:#A020F0;">'winperfspeed'</span><span style="color: #080;">&#41;</span>
Speed <span style="color: #0000FF;">is</span> 3.579545e+006 cycles/<span style="color: #0000FF;">sec</span>.
&nbsp;
&gt;&gt; feature<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'timing'</span>,<span style="color:#A020F0;">'wintime'</span><span style="color: #080;">&#41;</span>
Time <span style="color: #0000FF;">is</span> <span style="color: #33f;">1997625531</span> msec.
&nbsp;
&gt;&gt; feature<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'timing'</span>,<span style="color:#A020F0;">'wintimeofday'</span><span style="color: #080;">&#41;</span>
Time <span style="color: #0000FF;">is</span> <span style="color: #33f;">129543477454370000</span> units <span style="color: #080;">&#40;</span><span style="color: #33f;">100</span> nsec<span style="color: #080;">&#41;</span>.
&nbsp;
&gt;&gt; feature<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'timing'</span>,<span style="color:#A020F0;">'clocks_per_sec'</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">clock</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> speed <span style="color: #0000FF;">is</span> 1.000000e+003 cycles/<span style="color: #0000FF;">sec</span>.
&nbsp;
&gt;&gt; feature<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'timing'</span>,<span style="color:#A020F0;">'cpuspeed'</span><span style="color: #080;">&#41;</span>
Current CPU speed <span style="color: #0000FF;">is</span> 2.535746e+009 cycles/<span style="color: #0000FF;">sec</span>.</pre></div></div><p>All these features return a numeric value, if requested:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; cpuspeed = feature<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'timing'</span>,<span style="color:#A020F0;">'cpuspeed'</span><span style="color: #080;">&#41;</span>
cpuspeed =
          <span style="color: #33f;">2535702174.31193</span></pre></div></div><p>In fact, <code>feature('timing','cpucount')</code> is used internally by the built-in <i><b>tempname</b></i> function, to generate a unique temporary filename:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">if</span> usejava<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'jvm'</span><span style="color: #080;">&#41;</span>
    tmp_name = <span style="color: #0000FF;">fullfile</span><span style="color: #080;">&#40;</span>dirname, <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'tp'</span> <span style="color: #0000FF;">strrep</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">char</span><span style="color: #080;">&#40;</span>java.<span style="">util</span>.<span style="">UUID</span>.<span style="">randomUUID</span><span style="color: #080;">&#41;</span>,<span style="color:#A020F0;">'-'</span>,<span style="color:#A020F0;">'_'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">else</span>
    tmp_name = <span style="color: #0000FF;">fullfile</span><span style="color: #080;">&#40;</span>dirname, <span style="color: #080;">&#91;</span><span style="color:#A020F0;">'tp'</span> <span style="color: #0000FF;">num2str</span><span style="color: #080;">&#40;</span>feature<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'timing'</span>,<span style="color:#A020F0;">'cpucount'</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">end</span></pre></div></div><p><code>feature('timing','wintime')</code> returns the system time in nano-seconds, that can also be gotten directly via Java:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; uint64<span style="color: #080;">&#40;</span>java.<span style="">lang</span>.<span style="">System</span>.<span style="">nanoTime</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF;">ans</span> =
     <span style="color: #33f;">1997449616528637</span></pre></div></div><p>It may be interesting to learn the nuances between &#8216;wintime&#8217;, &#8216;wintimeofday&#8217; and <code>java.lang.System.currentTimeMillis</code>, which returns yet another value. If anyone knows, please <a
href="http://UndocumentedMatlab.com/blog/more-undocumented-timing-features/#respond">post a comment</a>.</p><h3 id="startup">-timing startup option</h3><p>Finally, note that Matlab has had a startup (command-line) option of <code>-timing</code> for a long time. This was documented up to <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/releases/R2009a/techdoc/ref/matlabunix.html">R2009a</a>, but removed from the documentation in <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/help/releases/R2009b/techdoc/ref/matlabunix.html">R2009b</a>, although the functionality remains to this day.</p><p>If Matlab is started with the <code>-timing</code> command-line option, it creates a log file of the time it took different segments of its startup process. Matlab displays the log file contents in the Matlab Command Window when initialization is done. This could help debug the numerous startup problems that users on a variety of platforms and system configurations report:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">  Toolbox <span style="color: #0000FF;">Path</span> Cache read in <span style="color: #33f;">0.08</span> seconds.
  <span style="">MATLAB</span> <span style="color: #0000FF;">Path</span> initialized in <span style="color: #33f;">1.27</span> seconds.
&nbsp;
<span style="">Opening</span> timing <span style="color: #0000FF;">log</span> C<span style="color: #F0F;">:</span>\DOCUME~<span style="color: #33f;">1</span>\Yair\LOCALS~<span style="color: #33f;">1</span>\Temp\timing_log.9104 ..
    <span style="">MATLAB</span> Startup Performance Metrics <span style="color: #080;">&#40;</span>In Seconds<span style="color: #080;">&#41;</span>
&nbsp;
total   item     gap      description         <span style="color: #228B22;">% Yair's comments/hunches</span>
=====================================         <span style="color: #228B22;">% =================================</span>
 <span style="color: #33f;">0.00</span>   <span style="color: #33f;">0.00</span>    <span style="color: #33f;">0.00</span>   MATLAB <span style="color: #0000FF;">script</span>          <span style="color: #228B22;">% A: Starting point</span>
 <span style="color: #33f;">5.70</span>   <span style="color: #33f;">5.70</span>    <span style="color: #33f;">0.00</span>   main                   <span style="color: #228B22;">% B: (??? - why so long after the initial Matlab script started?)</span>
 <span style="color: #33f;">6.14</span>   <span style="color: #33f;">0.08</span>    <span style="color: #33f;">0.36</span>   LM Startup             <span style="color: #228B22;">% C: License-manager check of the license validity (from B: 5.70+0.36+0.08=6.14)</span>
 <span style="color: #33f;">6.21</span>   <span style="color: #33f;">0.00</span>    <span style="color: #33f;">0.07</span>   splash                 <span style="color: #228B22;">% D: Matlab splash screen (can be bypassed with the '-nosplash' startup option) - (from C: 6.14+0.07=6.21)</span>
 <span style="color: #33f;">6.32</span>   <span style="color: #33f;">0.00</span>    <span style="color: #33f;">0.11</span>   mnSigInit              <span style="color: #228B22;">% E: (start the initialization part ???) - (from D: 6.21+0.11=6.32)</span>
 <span style="color: #33f;">7.84</span>   <span style="color: #33f;">1.49</span>    <span style="color: #33f;">0.02</span>      InitSunVM           <span style="color: #228B22;">% F: Start the Java Virtual Machine (JVM) used by Matlab GUI (from E: 6.32+0.02+1.49=7.84)</span>
<span style="color: #33f;">12.66</span>   <span style="color: #33f;">1.79</span>    <span style="color: #33f;">3.03</span>      PostVMInit          <span style="color: #228B22;">% G: End of JVM initialization (from F: 7.84+3.03+1.79=12.66)</span>
<span style="color: #33f;">12.67</span>   <span style="color: #33f;">6.35</span>    <span style="color: #33f;">0.01</span>     mljInit              <span style="color: #228B22;">% H: Initialization of Matlab's Java portion (from E: 6.32+0.01+6.35=12.67)</span>
<span style="color: #33f;">13.64</span>   <span style="color: #33f;">0.97</span>    <span style="color: #33f;">0.00</span>     StartDesktop         <span style="color: #228B22;">% I: Start to prepare &amp; initialize the Matlab Desktop (workspace, editor etc.) - (from H: 12.67+0.97=13.64)</span>
<span style="color: #33f;">13.64</span>   <span style="color: #33f;">7.31</span>    <span style="color: #33f;">0.01</span>   Java initialization    <span style="color: #228B22;">% J: ??? (from E: 6.32+0.01+7.31=13.64)</span>
<span style="color: #33f;">14.04</span>   <span style="color: #33f;">0.40</span>    <span style="color: #33f;">0.00</span>   hgInitialize           <span style="color: #228B22;">% K: Handle-Graphics initialization (from J: 13.64+0.40=14.04)</span>
<span style="color: #33f;">15.13</span>   <span style="color: #33f;">0.98</span>    <span style="color: #33f;">0.12</span>   psParser               <span style="color: #228B22;">% L: ??? (from K: 14.04+0.12+0.98=15.13)</span>
<span style="color: #33f;">15.67</span>   <span style="color: #33f;">0.07</span>    <span style="color: #33f;">0.46</span>   cachepath              <span style="color: #228B22;">% M: Toolbox Path Cache initialization (from L: 15.13+0.46+0.07=15.67)</span>
<span style="color: #33f;">18.53</span>   <span style="color: #33f;">1.27</span>    <span style="color: #33f;">1.60</span>     matlabpath           <span style="color: #228B22;">% N: Matlab path initialization (from M: 15.67+1.60+1.27=18.53)</span>
<span style="color: #33f;">21.54</span>   <span style="color: #33f;">0.00</span>    <span style="color: #33f;">3.01</span>     matlabpath           <span style="color: #228B22;">% O: ??? (from N: 18.53+3.01=21.52)</span>
<span style="color: #33f;">36.78</span>  <span style="color: #33f;">23.14</span>   <span style="color: #33f;">13.64</span>   Init Desktop           <span style="color: #228B22;">% P: I believe this indicates the end of the Desktop's init (from I: 13.64+23.14=36.78)</span>
<span style="color: #33f;">37.05</span>  <span style="color: #33f;">21.38</span>    <span style="color: #33f;">0.00</span>   <span style="color: #0000FF;">matlabrc</span>               <span style="color: #228B22;">% Q: indicates the end (?) of the m-file (matlabrc.m) initialization process (from M: 15.67+21.38=37.05)</span>
=====================================
Items shown account <span style="color: #0000FF;">for</span> <span style="color: #33f;">159.4</span><span style="color: #228B22;">% of total startup time [TIMER: 3 MHz]</span></pre></div></div><p>In the above list, note the unexplained gaps between the items. I&#8217;m not sure I understand them correctly. I posted my hunches as comments next to the relevant lines. I am not sure in some items whether they refer to the start or the end of their associated functionality. If anyone has other ideas or insight that will improve understanding of this list, please <a
href="http://UndocumentedMatlab.com/blog/more-undocumented-timing-features/#respond">share</a>.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/tic-toc-undocumented-option/' rel='bookmark' title='tic / toc &#8211; undocumented option'>tic / toc &#8211; undocumented option</a> <small>Matlab's built-in tic/toc functions have an undocumented option enabling multiple nested clockings...</small></li><li><a
href='http://undocumentedmatlab.com/blog/undocumented-feature-function/' rel='bookmark' title='Undocumented feature() function'>Undocumented feature() function</a> <small>Matlab's undocumented feature function enables access to some internal experimental features...</small></li><li><a
href='http://undocumentedmatlab.com/blog/undocumented-scatter-plot-behavior/' rel='bookmark' title='Undocumented scatter plot behavior'>Undocumented scatter plot behavior</a> <small>The scatter plot function has an undocumented behavior when plotting more than 100 points: it returns a single unified patch object handle, rather than a patch handle for each specific...</small></li><li><a
href='http://undocumentedmatlab.com/blog/types-of-undocumented-matlab-aspects/' rel='bookmark' title='Types of undocumented Matlab aspects'>Types of undocumented Matlab aspects</a> <small>This article lists the different types of undocumented/unsupported/hidden aspects in Matlab...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/more-undocumented-timing-features/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Datenum performance</title><link>http://undocumentedmatlab.com/blog/datenum-performance/</link> <comments>http://undocumentedmatlab.com/blog/datenum-performance/#comments</comments> <pubDate>Wed, 04 May 2011 18:00:34 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Medium risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Undocumented function]]></category> <category><![CDATA[datenum]]></category> <category><![CDATA[Internal component]]></category> <category><![CDATA[Performance]]></category> <category><![CDATA[Profiler]]></category> <category><![CDATA[Pure Matlab]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=2299</guid> <description><![CDATA[The performance of the built-in Matlab function datenum can be significantly improved by using an undocumented internal help function<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/datestr-performance/' rel='bookmark' title='datestr performance'>datestr performance</a> <small>Caching is a simple and very effective means to improve code performance, as demonstrated for the datestr function....</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-performance/' rel='bookmark' title='Plot performance'>Plot performance</a> <small>Undocumented inner plot mechanisms can be used to significantly improved plotting performance...</small></li><li><a
href='http://undocumentedmatlab.com/blog/performance-scatter-vs-line/' rel='bookmark' title='Performance: scatter vs. line'>Performance: scatter vs. line</a> <small>In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster...</small></li><li><a
href='http://undocumentedmatlab.com/blog/cellfun-undocumented-performance-boost/' rel='bookmark' title='cellfun &#8211; undocumented performance boost'>cellfun &#8211; undocumented performance boost</a> <small>Matlab's built-in cellfun function has an undocumented option to significantly improve performance in some cases....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>A few days ago, a reader on StackOverflow <a
target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/5818583/faster-function-than-datenum-in-matlab">asked</a> whether it is possible to improve the performance of Matlab&#8217;s built-in <i><b>datenum</b></i> function. This question reminded me of a similar case that I answered exactly two years ago, of <a
target="_blank" href="http://undocumentedmatlab.com/blog/ismembc-undocumented-helper-function/">improving the performance of the built-in <i><b>ismember</b></i> function</a>.</p><p>In both cases, the solution to the performance question can be found by simply using Matlab&#8217;s built-in profiler in order to extract just the core processing functionality. It is often found that in a particular situation there is no need for all the input arguments data validity checks, and under some known limitations we can indeed use the core functionality directly.</p><p>In the case of <i><b>ismember</b></i>, it turned out that if we are assured in advance that the input data are sorted non-sparse non-NaN values, then we can use the undocumented built-in helper functions <i><b>ismembc</b></i> or <i><b>ismembc2</b></i> for much-improved performance over the standard <i><b>ismember</b></i>. Both <i><b>ismembc</b></i> and <i><b>ismembc2</b></i> happen to be mex files, although this is not always the case for helper functions.</p><p>Our <i><b>datenum</b></i> case is very similar. It turns out that <i><b>datenum</b></i> uses the undocumented built-in helper function <i><b>dtstr2dtnummx</b></i> for the actual processing &#8211; converting a date from text to floating-point number. As I noted in <a
target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/5818583/faster-function-than-datenum-in-matlab/5823278#5823278">my response</a> to the StackOverflow question, we can directly use this helper function for improved performance: On my particular computer, <i><b>dtstr2dtnummx</b></i> is over 3 times faster than the standard <i><b>datenum</b></i> function:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #228B22;">% Fast - using dtstr2dtnummx</span>
&gt;&gt; <span style="color: #0000FF;">tic</span>, <span style="color: #0000FF;">for</span> <span style="color: #0000FF;"><span style="color: #33f;">i</span></span>=<span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">1000</span>; <span style="color: #0000FF;">dateNum</span>=dtstr2dtnummx<span style="color: #080;">&#40;</span><span style="color: #080;">&#123;</span><span style="color:#A020F0;">'2010-12-12 12:21:12.123'</span><span style="color: #080;">&#125;</span>,<span style="color:#A020F0;">'yyyy-MM-dd HH:mm:ss'</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">end</span>; <span style="color: #0000FF;">dateNum</span>,<span style="color: #0000FF;">toc</span>
<span style="color: #0000FF;">dateNum</span> =
          <span style="color: #33f;">734484.514722222</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.218423</span> seconds.
&nbsp;
<span style="color: #228B22;">% Slower - using datenum</span>
&gt;&gt; <span style="color: #0000FF;">tic</span>, <span style="color: #0000FF;">for</span> <span style="color: #0000FF;"><span style="color: #33f;">i</span></span>=<span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">1000</span>; <span style="color: #0000FF;">dateNum</span>=<span style="color: #0000FF;">datenum</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#123;</span><span style="color:#A020F0;">'2010-12-12 12:21:12.123'</span><span style="color: #080;">&#125;</span>,<span style="color:#A020F0;">'yyyy-mm-dd HH:MM:SS'</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">end</span>; <span style="color: #0000FF;">dateNum</span>,<span style="color: #0000FF;">toc</span>
<span style="color: #0000FF;">dateNum</span> =
          <span style="color: #33f;">734484.514722222</span>   <span style="color: #228B22;">% Same value as dtstr2dtnummx - good!</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.658352</span> seconds.   <span style="color: #228B22;">% 3x slower than dtstr2dtnummx - bad!</span></pre></div></div><p>While the difference in timing may appear negligible, if you are using this function to parse a text file with thousands of lines, each with its own timestamp, then these seemingly negligible time differences quickly add up. Of course, this only makes sense to do if you find out (using the profiler again) that this date parsing is a performance hotspot in your particular application. It was indeed such a performance hotspot in one of my applications, as it apparently was also for the original poster on StackOverflow.</p><p>Like <i><b>ismembc</b></i>, <i><b>dtstr2dtnummx</b></i> is an internal mex function. On my Windows system it is located in C:\Program Files\Matlab\R2011a\toolbox\matlab\timefun\private\dtstr2dtnummx.mexw32. It will have a different extension non-Windows systems, but you will easily find it in its containing folder.</p><p>To gain access to <i><b>dtstr2dtnummx</b></i>, simply add its folder to the Matlab path using the <i><b>addpath</b></i> function, or copy the dtstr2dtnummx.mexw32 file to another folder that is already on your Matlab path.</p><p>Note that the string format is different between <i><b>dtstr2dtnummx</b></i> and <i><b>datenum</b></i>: In the test case above, <i><b>dtstr2dtnummx</b></i> used <code>'yyyy-MM-dd HH:mm:ss'</code>, while <i><b>datenum</b></i> required <code>'yyyy-<b>mm</b>-dd HH:<b>MM:SS</b>'</code>. I have no idea why MathWorks did not keep consistent formatting strings. But because of this, we need to be extra careful (<a
target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/5831563/matlab-date-format/">example1</a>, <a
target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/5880242/matlab-datenum-generation">example2</a>). If you are interested in finding out how the <i><b>datenum</b></i> format strings translates into a <i><b>dtstr2dtnummx</b></i>, take a look at the helper function <i><b>cnv2icudf</b></i>, which is a very readable m-file located in the same folder as <i><b>dtstr2dtnummx</b></i>.</p><p>To those interested, the folder that contains <i><b>dtstr2dtnummx</b></i> also contains some other interesting date conversion functions, so explore and enjoy!</p><p>Perhaps the main lesson that can be learned from this article, and its <i><b>ismembc</b></i> predecessor of two years ago, is that it is very useful to profile the code for performance hotspots. When such a hotspot is found, don&#8217;t stop your profiling at the built-in Matlab functions &#8211; keep digging in the profiler results and perhaps you&#8217;ll find that you can improve performance by taking an internal shortcut.</p><p>Have you discovered any other performance shortcuts in a built-in Matlab function? If so, please <a
href="http://undocumentedmatlab.com/blog/datenum-performance/#respond">post a comment</a> to tell us all about it.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/datestr-performance/' rel='bookmark' title='datestr performance'>datestr performance</a> <small>Caching is a simple and very effective means to improve code performance, as demonstrated for the datestr function....</small></li><li><a
href='http://undocumentedmatlab.com/blog/plot-performance/' rel='bookmark' title='Plot performance'>Plot performance</a> <small>Undocumented inner plot mechanisms can be used to significantly improved plotting performance...</small></li><li><a
href='http://undocumentedmatlab.com/blog/performance-scatter-vs-line/' rel='bookmark' title='Performance: scatter vs. line'>Performance: scatter vs. line</a> <small>In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster...</small></li><li><a
href='http://undocumentedmatlab.com/blog/cellfun-undocumented-performance-boost/' rel='bookmark' title='cellfun &#8211; undocumented performance boost'>cellfun &#8211; undocumented performance boost</a> <small>Matlab's built-in cellfun function has an undocumented option to significantly improve performance in some cases....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/datenum-performance/feed/</wfw:commentRss> <slash:comments>7</slash:comments> </item> <item><title>Plot performance</title><link>http://undocumentedmatlab.com/blog/plot-performance/</link> <comments>http://undocumentedmatlab.com/blog/plot-performance/#comments</comments> <pubDate>Wed, 16 Jun 2010 09:55:47 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[Low risk of breaking in future versions]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Undocumented feature]]></category> <category><![CDATA[Performance]]></category> <category><![CDATA[Profiler]]></category> <category><![CDATA[Pure Matlab]]></category> <category><![CDATA[Undocumented property]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1623</guid> <description><![CDATA[Undocumented inner plot mechanisms can be used to significantly improved plotting performance<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/plot-liminclude-properties/' rel='bookmark' title='Plot LimInclude properties'>Plot LimInclude properties</a> <small>The plot objects' XLimInclude, YLimInclude, ZLimInclude, ALimInclude and CLimInclude properties are an important feature, that has both functional and performance implications....</small></li><li><a
href='http://undocumentedmatlab.com/blog/undocumented-scatter-plot-behavior/' rel='bookmark' title='Undocumented scatter plot behavior'>Undocumented scatter plot behavior</a> <small>The scatter plot function has an undocumented behavior when plotting more than 100 points: it returns a single unified patch object handle, rather than a patch handle for each specific...</small></li><li><a
href='http://undocumentedmatlab.com/blog/controlling-plot-data-tips/' rel='bookmark' title='Controlling plot data-tips'>Controlling plot data-tips</a> <small>Data-tips are an extremely useful plotting tool that can easily be controlled programmatically....</small></li><li><a
href='http://undocumentedmatlab.com/blog/performance-scatter-vs-line/' rel='bookmark' title='Performance: scatter vs. line'>Performance: scatter vs. line</a> <small>In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster...</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>I recently consulted to a client who wanted to display an interactive plot with numerous data points that kept updating in real-time. Matlab&#8217;s standard plotting functions simply could not keep up with the rate of data change. Today, I want to share a couple of very simple undocumented hacks that significantly improve plotting performance and fixed my problem.</p><p>I begin by stating the obvious: whenever possible, try to <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/support/tech-notes/1100/1109.html">vectorize</a> your code, <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f8-784135.html">preallocate the data</a> and other <a
target="_blank" rel="nofollow" href="http://www.mathworks.com/support/solutions/en/data/1-15NM7/">performance-improving techniques suggested by Matlab</a>. Unfortunately, sometimes (as in my specific case above) all these cannot help. Even in such cases, we can still find important performance tricks, such as these:</p><h3 id="limits">Performance hack #1: manual limits</h3><p>Whenever Matlab updates plot data, it checks whether any modification needs to be done to any of its limits. This computation-intensive task is done for any limit that is set to &#8216;Auto&#8217; mode, which is the default axes limits mode. If instead we manually set the axes limits to the requested range, Matlab skips these checks, enabling much faster plotting performance.</p><p>Let us simulate the situation by adding 500 data points to a plot, one at a time:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; x=<span style="color: #33f;">0</span><span style="color: #F0F;">:</span><span style="color: #33f;">0.02</span><span style="color: #F0F;">:</span><span style="color: #33f;">10</span>; y=<span style="color: #0000FF;">sin</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span>;
&gt;&gt; <span style="color: #0000FF;">clf</span>; <span style="color: #0000FF;">cla</span>; <span style="color: #0000FF;">tic</span>;
&gt;&gt; <span style="color: #0000FF;">drawnow</span>; <span style="color: #0000FF;">plot</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>,y<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">hold</span> on; <span style="color: #0000FF;">legend</span> data;
&gt;&gt; <span style="color: #0000FF;">for</span> idx = <span style="color: #33f;">1</span> <span style="color: #F0F;">:</span> <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">plot</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#40;</span>idx<span style="color: #080;">&#41;</span>,y<span style="color: #080;">&#40;</span>idx<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">drawnow</span>; <span style="color: #0000FF;">end</span>;
&gt;&gt; <span style="color: #0000FF;">toc</span>
&nbsp;
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">21.583668</span> seconds.</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 321px"><img
alt="simple plot with 500 data points" src="http://UndocumentedMatlab.com/images/Plot.png" title="simple plot with 500 data points" width="311" height="286" /><p
class="wp-caption-text">simple plot with 500 data points</p></div></center></p><p>And now let&#8217;s use static axes limits:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; x=<span style="color: #33f;">0</span><span style="color: #F0F;">:</span><span style="color: #33f;">0.02</span><span style="color: #F0F;">:</span><span style="color: #33f;">10</span>; y=<span style="color: #0000FF;">sin</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span>;
&gt;&gt; <span style="color: #0000FF;">clf</span>; <span style="color: #0000FF;">cla</span>; <span style="color: #0000FF;">tic</span>; <span style="color: #0000FF;">drawnow</span>; 
&gt;&gt; <span style="color: #0000FF;">plot</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>,y<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">hold</span> on; <span style="color: #0000FF;">legend</span> data;
&nbsp;
&gt;&gt; <span style="color: #0000FF;">xlim</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color: #33f;">0</span>,<span style="color: #33f;">10</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">ylim</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span>-<span style="color: #33f;">1</span>,<span style="color: #33f;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% static limits</span>
&nbsp;
&gt;&gt; <span style="color: #0000FF;">for</span> idx = <span style="color: #33f;">1</span> <span style="color: #F0F;">:</span> <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">plot</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#40;</span>idx<span style="color: #080;">&#41;</span>,y<span style="color: #080;">&#40;</span>idx<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">drawnow</span>; <span style="color: #0000FF;">end</span>;
&gt;&gt; <span style="color: #0000FF;">toc</span>
&nbsp;
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">16.863090</span> seconds.</pre></div></div><p>Note that this trick is the basis for the performance improvement that occurs when using the plot&#8217;s <a
target="_blank" href="http://UndocumentedMatlab.com/blog/plot-liminclude-properties/">undocumented set of <b>LimInclude</b> properties</a>.</p><p>Of course, setting manual limits prevents the axes limits from growing and shrinking automatically with the data, which can actually be a very useful feature sometimes. But if performance is important, we now know that we have this tool to improve it.</p><h3 id="Legend">Performance hack #2: static legend</h3><p>Hack #1 gave us a 22% performance boost, but we can do much better. Running the <a
target="_blank" href="http://UndocumentedMatlab.com/blog/undocumented-profiler-options/">profiler</a> on the code above we see that much of the time is spent recomputing the legend. Looking inside the legend code (specifically, the <i><b>legendcolorbarlayout</b></i> function), we detect several short-circuits that we can use to make the legend static and prevent recomputation:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; x=<span style="color: #33f;">0</span><span style="color: #F0F;">:</span><span style="color: #33f;">0.02</span><span style="color: #F0F;">:</span><span style="color: #33f;">10</span>; y=<span style="color: #0000FF;">sin</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span>;
&gt;&gt; <span style="color: #0000FF;">clf</span>; <span style="color: #0000FF;">cla</span>; <span style="color: #0000FF;">tic</span>; <span style="color: #0000FF;">drawnow</span>; 
&gt;&gt; <span style="color: #0000FF;">plot</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>,y<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">hold</span> on; <span style="color: #0000FF;">legend</span> data;
&nbsp;
&gt;&gt; <span style="color: #0000FF;">xlim</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color: #33f;">0</span>,<span style="color: #33f;">10</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">ylim</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span>-<span style="color: #33f;">1</span>,<span style="color: #33f;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;  <span style="color: #228B22;">% static limits</span>
&nbsp;
&gt;&gt; <span style="color: #228B22;">% Static legend</span>
&gt;&gt; <span style="color: #0000FF;">set</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gca</span>,<span style="color:#A020F0;">'LegendColorbarListeners'</span>,<span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>; 
&gt;&gt; <span style="color: #0000FF;">setappdata</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gca</span>,<span style="color:#A020F0;">'LegendColorbarManualSpace'</span>,<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
&gt;&gt; <span style="color: #0000FF;">setappdata</span><span style="color: #080;">&#40;</span><span style="color: #0000FF;">gca</span>,<span style="color:#A020F0;">'LegendColorbarReclaimSpace'</span>,<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
&nbsp;
&gt;&gt; <span style="color: #0000FF;">for</span> idx = <span style="color: #33f;">1</span> <span style="color: #F0F;">:</span> <span style="color: #0000FF;">length</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">plot</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#40;</span>idx<span style="color: #080;">&#41;</span>,y<span style="color: #080;">&#40;</span>idx<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">drawnow</span>; <span style="color: #0000FF;">end</span>;
&gt;&gt; <span style="color: #0000FF;">toc</span>
&nbsp;
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">5.209053</span> seconds.</pre></div></div><p>Now this is much much better &#8211; a 76% performance boost compared to the original plot (i.e., 4 times faster!). Of course, it prevents the legend from being dynamically updated. Sometimes we actually wish for this dynamic effect (last year I explained how to use the <a
target="_blank" href="http://UndocumentedMatlab.com/blog/legend-semi-documented-feature/">legend&#8217;s undocumented -DynamicLegend feature</a> for even greater dynamic control). But when performance is important, we can still display a legend without its usual performance cost.</p><p>In conclusion, I have demonstrated that Matlab performance can often be improved significantly, even in the absence of any vectorization, by simply understanding the internal mechanisms and bypassing those which are irrelevant in our specific case.</p><p>Have you found other similar performance hacks? If so, please share them in the <a
href="#respond">comments section</a> below.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/plot-liminclude-properties/' rel='bookmark' title='Plot LimInclude properties'>Plot LimInclude properties</a> <small>The plot objects' XLimInclude, YLimInclude, ZLimInclude, ALimInclude and CLimInclude properties are an important feature, that has both functional and performance implications....</small></li><li><a
href='http://undocumentedmatlab.com/blog/undocumented-scatter-plot-behavior/' rel='bookmark' title='Undocumented scatter plot behavior'>Undocumented scatter plot behavior</a> <small>The scatter plot function has an undocumented behavior when plotting more than 100 points: it returns a single unified patch object handle, rather than a patch handle for each specific...</small></li><li><a
href='http://undocumentedmatlab.com/blog/controlling-plot-data-tips/' rel='bookmark' title='Controlling plot data-tips'>Controlling plot data-tips</a> <small>Data-tips are an extremely useful plotting tool that can easily be controlled programmatically....</small></li><li><a
href='http://undocumentedmatlab.com/blog/performance-scatter-vs-line/' rel='bookmark' title='Performance: scatter vs. line'>Performance: scatter vs. line</a> <small>In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster...</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/plot-performance/feed/</wfw:commentRss> <slash:comments>15</slash:comments> </item> </channel> </rss>

<!-- W3 Total Cache: Minify debug info:
Engine:             disk: basic
Theme:              b7666
Template:           tag
-->
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: undocumentedmatlab.com @ 2012-05-21 20:16:22 -->

<!-- W3 Total Cache: Page cache debug info:
Engine:             disk: enhanced
Cache key:          blog/tag/performance/feed/_index.xml_gzip
Caching:            enabled
Status:             not cached
Creation Time:      2.050s
Header info:
X-Pingback:         http://undocumentedmatlab.com/blog/xmlrpc.php
Set-Cookie:         wpgb_visit_last_php-default=1337656581; expires=Wed, 22-May-2013 03:16:21 GMT; path=/
Content-Type:       text/xml; charset=UTF-8
Last-Modified:      Tue, 22 May 2012 03:16:22 GMT
Vary:               Accept-Encoding, Cookie
Expires:            Tue, 22 May 2012 04:16:22 GMT
Pragma:             public
Cache-Control:      public, must-revalidate, proxy-revalidate
Etag:               17a79c9a6be9109cde0a46b1ce54635c
Content-Encoding:   gzip
-->
