<?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, 02 Feb 2012 00:24:18 +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>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/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/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><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>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/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/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><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/matlab-java-memory-leaks-performance/feed/</wfw:commentRss> <slash:comments>9</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/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><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>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/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><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/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/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></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/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></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/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/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/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></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/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/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/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></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/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/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/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></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/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/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/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></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/plot-performance/feed/</wfw:commentRss> <slash:comments>15</slash:comments> </item> <item><title>Plot LimInclude properties</title><link>http://undocumentedmatlab.com/blog/plot-liminclude-properties/</link> <comments>http://undocumentedmatlab.com/blog/plot-liminclude-properties/#comments</comments> <pubDate>Wed, 31 Mar 2010 19:58:01 +0000</pubDate> <dc:creator>Yair Altman</dc:creator> <category><![CDATA[Handle graphics]]></category> <category><![CDATA[Hidden property]]></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[Pure Matlab]]></category> <category><![CDATA[Undocumented property]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=1258</guid> <description><![CDATA[The plot objects' XLimInclude, YLimInclude, ZLimInclude, ALimInclude and CLimInclude properties are an important feature, that has both functional and performance implications.<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/borderless-button-used-for-plot-properties/' rel='bookmark' title='Borderless button used for plot properties'>Borderless button used for plot properties</a> <small>A borderless button can be used to add unobtrusive functionality to plot axes...</small></li><li><a
href='http://undocumentedmatlab.com/blog/getundoc-get-undocumented-object-properties/' rel='bookmark' title='getundoc &#8211; get undocumented object properties'>getundoc &#8211; get undocumented object properties</a> <small>getundoc is a very simple utility that displays the hidden (undocumented) properties of a specified handle object....</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/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></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Concluding my three-part mini-series on hidden and undocumented plot/axes properties, I would like to present a set of properties that I find very useful in dynamic plots: <b>XLimInclude, YLimInclude, ZLimInclude, ALimInclude</b> and <b>CLimInclude</b>. These properties, which are relevant for plot/axes objects, have an &#8216;on&#8217; value by default. When set to &#8216;off&#8217;, they exclude their object from the automatic computation of the corresponding axes limits (<b>XLim/YLim/ZLim/ALim/CLim</b>).</p><p>For example, here&#8217;s a simple sine wave with a wavefront line marker. Note how the too-tall wavefront line affects the entire axes Y-limits:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">cla</span>;
t=<span style="color: #33f;">0</span><span style="color: #F0F;">:</span>.01<span style="color: #F0F;">:</span><span style="color: #33f;">7.5</span>;
<span style="color: #0000FF;">plot</span><span style="color: #080;">&#40;</span>t,<span style="color: #0000FF;">sin</span><span style="color: #080;">&#40;</span>t<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">line</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'xdata'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">7.5</span>,<span style="color: #33f;">7.5</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'ydata'</span>,<span style="color: #080;">&#91;</span>-<span style="color: #33f;">5</span>,<span style="color: #33f;">5</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'color'</span>,<span style="color:#A020F0;">'r'</span><span style="color: #080;">&#41;</span>; 
<span style="color: #0000FF;">box</span> off</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 327px"><img
alt="Regular plot (YLimInclude on)" src="http://UndocumentedMatlab.com/images/YLimInclude_before_small.png" title="Regular plot (YLimInclude on)" width="317" height="271" /><p
class="wp-caption-text">Regular plot (YLimInclude on)</p></div></center></p><p>This situation is quickly fixed using the <b>YLimInclude</b> property:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">cla</span>;
t=<span style="color: #33f;">0</span><span style="color: #F0F;">:</span>.01<span style="color: #F0F;">:</span><span style="color: #33f;">7.5</span>;
<span style="color: #0000FF;">plot</span><span style="color: #080;">&#40;</span>t,<span style="color: #0000FF;">sin</span><span style="color: #080;">&#40;</span>t<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;
<span style="color: #0000FF;">line</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'xdata'</span>,<span style="color: #080;">&#91;</span><span style="color: #33f;">7.5</span>,<span style="color: #33f;">7.5</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'ydata'</span>,<span style="color: #080;">&#91;</span>-<span style="color: #33f;">5</span>,<span style="color: #33f;">5</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'color'</span>,<span style="color:#A020F0;">'r'</span>, <span style="color: #F0F;">...</span>
     <span style="color:#A020F0;">'YLimInclude'</span>,<span style="color:#A020F0;">'off'</span><span style="color: #080;">&#41;</span>; 
<span style="color: #0000FF;">box</span> off</pre></div></div><p><center><div
class="wp-caption aligncenter" style="width: 327px"><img
alt="YLimInclude off" src="http://UndocumentedMatlab.com/images/YLimInclude_after_small.png" title="YLimInclude off" width="317" height="271" /><p
class="wp-caption-text">YLimInclude off</p></div></center></p><p>Beside the functional importance of this feature, it also has a large potential for improved application performance: I recently designed a monitor-like GUI for a medical application, where the data is constantly updated from an external sensor connected to the computer. The GUI presents the latest 10 seconds of monitored data, which bounce up and down the chart. A red wave-front line is presented and constantly updated, to indicate the current data position. Since the monitored data jumps up and down, the Y-limits of the monitor chart often changes, and with it I would need to modify the wavefront&#8217;s <b>YData</b> based on the updated axes <b>YLim</b>. This turned out to steal precious CPU time from the actual monitoring application. Came <b>YLimInclude</b> to the rescue, by letting me specify the wavefront line as:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">hWavefront = <span style="color: #0000FF;">line</span><span style="color: #080;">&#40;</span><span style="color: #F0F;">...</span>, <span style="color:#A020F0;">'YData'</span>,<span style="color: #080;">&#91;</span>-<span style="color: #33f;">99</span>,<span style="color: #33f;">99</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'YLimInclude'</span>,<span style="color:#A020F0;">'off'</span><span style="color: #080;">&#41;</span>;</pre></div></div><p>Now the wavefront line never needed to update its <b>YData</b> (only <b>XData</b>, which is much less CPU-intensive) &#8211; it always spanned the entire axes height, since [-99,99] were assured (in my particular case) to exceed the actual monitored data. This looked better (no flicker effects) and performed faster than the regular (documented) approach.</p><p>Note that although all these properties exist, to the best of my knowledge, for all Handle-Graphic plot objects, they are sometimes meaningless. For example, <b>ZLimInclude</b> is irrelevant for a 2D patchless plot; <b>CLimInclude</b> relates to the axes color limits which are irrelevant if you&#8217;re not using a <b><i>colormap</i></b> or something similar; <b>ALimInclude</b> relates to patch transparency (alpha-channel) and is irrelevant elsewhere. In these and similar cases, setting these properties, while allowed and harmless, will simply have no effect.</p><p>This concludes my mini-series of undocumented plot/axes properties. To recap, the other articles dealt with the <a
target="_blank" href="http://undocumentedmatlab.com/blog/axes-looseinset-property/"><strong>LooseInset</strong></a> and <a
target="_blank" href="http://undocumentedmatlab.com/blog/plot-linesmoothing-property/"><strong>LineSmoothing</strong></a> properties.</p><p>Have you found other similar properties or use-cases that you find useful? I will be most interested to read about them in the comments section below.</p><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/borderless-button-used-for-plot-properties/' rel='bookmark' title='Borderless button used for plot properties'>Borderless button used for plot properties</a> <small>A borderless button can be used to add unobtrusive functionality to plot axes...</small></li><li><a
href='http://undocumentedmatlab.com/blog/getundoc-get-undocumented-object-properties/' rel='bookmark' title='getundoc &#8211; get undocumented object properties'>getundoc &#8211; get undocumented object properties</a> <small>getundoc is a very simple utility that displays the hidden (undocumented) properties of a specified handle object....</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/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></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/plot-liminclude-properties/feed/</wfw:commentRss> <slash:comments>7</slash:comments> </item> <item><title>Performance: scatter vs. line</title><link>http://undocumentedmatlab.com/blog/performance-scatter-vs-line/</link> <comments>http://undocumentedmatlab.com/blog/performance-scatter-vs-line/#comments</comments> <pubDate>Wed, 14 Oct 2009 15:00:57 +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[Memory]]></category> <category><![CDATA[Stock Matlab function]]></category> <category><![CDATA[Performance]]></category> <category><![CDATA[Pure Matlab]]></category><guid
isPermaLink="false">http://undocumentedmatlab.com/?p=637</guid> <description><![CDATA[In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster<pre> </pre>Related posts:<ol><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/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><li><a
href='http://undocumentedmatlab.com/blog/multi-line-tooltips/' rel='bookmark' title='Multi-line tooltips'>Multi-line tooltips</a> <small>Multi-line tooltips are very easy to set up, once you know your way around a few undocumented hiccups....</small></li></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>Following my previous article on the <a
target="_blank" href="http://undocumentedmatlab.com/blog/undocumented-scatter-plot-behavior/">undocumented behavior of the scatter function</a>, one of my readers, Benoit Charles, reported a discovery that in many circumstances the <b><i>line</i></b> function generates identical plots much faster than <b><i>scatter</i></b>.</p><p>Unlike <b><i>scatter</i></b>, <b><i>line</i></b> does not enable specific data-point marker customization, although the colors could be modified. On the other hand, <b><i>line</i></b> only uses a single handle object, saving memory and system resources compared to <b><i>scatter</i></b> keeping a separate handle for each data point. So, if you just need to quickly plot a bunch of scattered points then <b><i>line</i></b> could be a better choice than <b><i>scatter</i></b>.</p><p>Here is a simple code snippet, which generates identical plots and shows the performance difference:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;">&gt;&gt; x=<span style="color: #0000FF;">rand</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1000</span>,<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>; y=<span style="color: #0000FF;">rand</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1000</span>,<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;
&nbsp;
&gt;&gt; <span style="color: #0000FF;">tic</span>, <span style="color: #0000FF;">for</span> idx=<span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">100</span>, <span style="color: #0000FF;">cla</span>; h=<span style="color: #0000FF;">scatter</span><span style="color: #080;">&#40;</span>x,y<span style="color: #080;">&#41;</span>; <span style="color: #0000FF;">end</span>; <span style="color: #0000FF;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">2.521322</span> seconds.
&nbsp;
&gt;&gt; props = <span style="color: #080;">&#123;</span><span style="color:#A020F0;">'LineStyle'</span>,<span style="color:#A020F0;">'none'</span>,<span style="color:#A020F0;">'Marker'</span>,<span style="color:#A020F0;">'o'</span>,<span style="color:#A020F0;">'MarkerEdge'</span>,<span style="color:#A020F0;">'b'</span>,<span style="color:#A020F0;">'MarkerSize'</span>,<span style="color: #33f;">6</span><span style="color: #080;">&#125;</span>;
&gt;&gt; <span style="color: #0000FF;">tic</span>, <span style="color: #0000FF;">for</span> idx=<span style="color: #33f;">1</span><span style="color: #F0F;">:</span><span style="color: #33f;">100</span>, <span style="color: #0000FF;">cla</span>; h=<span style="color: #0000FF;">line</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span>x,x<span style="color: #080;">&#93;</span>,<span style="color: #080;">&#91;</span>y,y<span style="color: #080;">&#93;</span>,props<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;">toc</span>
Elapsed time <span style="color: #0000FF;">is</span> <span style="color: #33f;">0.333369</span> seconds.</pre></div></div><p>In the past, I have posted about other undocumented performance aspects, <a
target="_blank" href="http://undocumentedmatlab.com/blog/ismembc-undocumented-helper-function/">comparing the documented ismember function with the undocumented ismembc</a> and about <a
target="_blank" href="http://undocumentedmatlab.com/blog/cellfun-undocumented-performance-boost/"><b><i>cellfun</i</b>&#8216;s undocumented options</a>. If you are aware of other similar functions having identical outputs and a significant performance difference, please let me know.</p><p><pre> </pre>Related posts:<ol><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/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><li><a
href='http://undocumentedmatlab.com/blog/multi-line-tooltips/' rel='bookmark' title='Multi-line tooltips'>Multi-line tooltips</a> <small>Multi-line tooltips are very easy to set up, once you know your way around a few undocumented hiccups....</small></li></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/performance-scatter-vs-line/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>tic / toc &#8211; undocumented option</title><link>http://undocumentedmatlab.com/blog/tic-toc-undocumented-option/</link> <comments>http://undocumentedmatlab.com/blog/tic-toc-undocumented-option/#comments</comments> <pubDate>Fri, 22 May 2009 13:12:05 +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=310</guid> <description><![CDATA[Matlab's built-in tic/toc functions have an undocumented option enabling multiple nested clockings<pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/more-undocumented-timing-features/' rel='bookmark' title='More undocumented timing features'>More undocumented timing features</a> <small>There are several undocumented ways in Matlab to get CPU and clock data...</small></li><li><a
href='http://undocumentedmatlab.com/blog/ismembc-undocumented-helper-function/' rel='bookmark' title='ismembc &#8211; undocumented helper function'>ismembc &#8211; undocumented helper function</a> <small>Matlab has several undocumented internal helper functions that can be useful on their own in some cases. This post presents the ismembc function....</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><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></ol><pre> </pre>]]></description> <content:encoded><![CDATA[<p>I have recently discussed <a
href="http://undocumentedmatlab.com/blog/undocumented-profiler-options/" target="_blank">undocumented features in Matlab&#8217;s built-in profiler</a>. Another tool often used for ad-hoc performance profiling of Matlab code are the built-in duo functions <em><strong>tic</strong></em> and <em><strong>toc</strong></em>:</p><blockquote><pre><span style="color: #000000;">&gt;&gt; tic; magic(500); toc</span>
Elapsed time is 0.474647 seconds.</pre></blockquote><p>Both <em><strong>tic</strong></em> and <em><strong>toc</strong></em> are documented functions, but they contain an undocumented option (at least until R2008b &#8211; see history below) that enables nested clocking. The problem that is solved using this undocumented option is that if we try to use <em><strong>tic/toc</strong></em> in external function <em>parentFcn</em> and also in the internal function <em>childFcn</em>, we get unexpected results. This happens because our <em>childFcn</em>&#8216;s invocation of <em><strong>tic</strong></em> has reset the clock and so <em>parentFcn</em>&#8216;s clock is now obviously incorrect:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> parentFcn
&nbsp;
    <span style="color: #228B22;">% Start clocking</span>
    <span style="color: #0000FF;">tic</span>;
&nbsp;
    <span style="color: #228B22;">% Do something long...</span>
    m1 = <span style="color: #0000FF;">magic</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1500</span><span style="color: #080;">&#41;</span>;
&nbsp;
    <span style="color: #228B22;">% Call nested function</span>
    toc1 = childFcn;
&nbsp;
    <span style="color: #228B22;">% Present clock timing</span>
    toc2 = <span style="color: #0000FF;">toc</span>;
    <span style="color: #0000FF;">fprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Elapsed time for childFcn:  %0.3f secs\n'</span>,toc1<span style="color: #080;">&#41;</span>
    <span style="color: #0000FF;">fprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Elapsed time for parentFcn: %0.3f secs\n'</span>,toc2<span style="color: #080;">&#41;</span>
&nbsp;
&nbsp;
    <span style="color: #228B22;">% Nested function with separate clocking</span>
    <span style="color: #0000FF;">function</span> t = childFcn
        <span style="color: #0000FF;">tic</span>;
        m = <span style="color: #0000FF;">magic</span><span style="color: #080;">&#40;</span><span style="color: #33f;">800</span><span style="color: #080;">&#41;</span>;
        t = <span style="color: #0000FF;">toc</span>;
    <span style="color: #0000FF;">end</span>  <span style="color: #228B22;">% childFcn</span>
&nbsp;
<span style="color: #0000FF;">end</span>  <span style="color: #228B22;">% parentFcn</span></pre></div></div><blockquote><pre><span style="color: #000000;">&gt;&gt; parentFcn</span>
Elapsed time for childFcn:  1.063 secs
Elapsed time for parentFcn: 1.068 secs
</pre></blockquote><p>The solution: use a separate handle for each clock. The format is very simple:</p><blockquote><pre><span style="color: #000000;">&gt;&gt; t = tic;
&gt;&gt; toc(t)</span>
</pre></blockquote><p>This format ensures independent clocking of the clocks. Clockings can now be safely nested and even partial overlap is possible:</p><div
class="wp_syntax"><div
class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">function</span> parentFcn
&nbsp;
    <span style="color: #228B22;">% Start clocking</span>
    t1 = <span style="color: #0000FF;">tic</span>;
&nbsp;
    <span style="color: #228B22;">% Do something long...</span>
    m1 = <span style="color: #0000FF;">magic</span><span style="color: #080;">&#40;</span><span style="color: #33f;">1500</span><span style="color: #080;">&#41;</span>;
&nbsp;
    <span style="color: #228B22;">% Call nested function</span>
    toc1 = childFcn;
&nbsp;
    <span style="color: #228B22;">% Present clock timing</span>
    toc2 = <span style="color: #0000FF;">toc</span><span style="color: #080;">&#40;</span>t1<span style="color: #080;">&#41;</span>;
    <span style="color: #0000FF;">fprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Elapsed time for childFcn:  %0.3f secs\n'</span>,toc1<span style="color: #080;">&#41;</span>
    <span style="color: #0000FF;">fprintf</span><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Elapsed time for parentFcn: %0.3f secs\n'</span>,toc2<span style="color: #080;">&#41;</span>
&nbsp;
&nbsp;
    <span style="color: #228B22;">% Nested function with separate clocking</span>
    <span style="color: #0000FF;">function</span> t = childFcn
        <span style="color: #0000FF;">tic</span>;
        m = <span style="color: #0000FF;">magic</span><span style="color: #080;">&#40;</span><span style="color: #33f;">800</span><span style="color: #080;">&#41;</span>;
        t = <span style="color: #0000FF;">toc</span>;
    <span style="color: #0000FF;">end</span>  <span style="color: #228B22;">% childFcn</span>
&nbsp;
<span style="color: #0000FF;">end</span>  <span style="color: #228B22;">% parentFcn</span></pre></div></div><blockquote><pre><span style="color: #000000;">&gt;&gt; parentFcn</span>
Elapsed time for childFcn:  1.123 secs
Elapsed time for parentFcn: 4.992 secs
</pre></blockquote><p>For the record, this undocumented option was first presented by the MathWorks support team as an <a
rel="nofollow" href="http://www.mathworks.com/support/solutions/data/1-3JINZY.html" target="_blank">official workaround for the aforementioned problem</a>. The solution was originally posted for Matlab R2006b, but actually the option was supported by Matlab versions at least as early as R14SP3 (7.1) &#8211; perhaps even earlier (I don&#8217;t have ready access to earlier versions). I do know that it was not supported on release R12 aka 6.0, but I don&#8217;t know whether it was introduced in 6.5, 7.0 or 7.1. This option has only been <a
rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/tic.html" target="_blank">documented</a><a></a> since R2008b, although it has existed in many earlier releases.</p><p>Now here&#8217;s a puzzle for all you undoc fans out there: <em><strong>toc</strong></em> has a really undocumented option of using an <em>uninitialized</em> <strong><em>uint64</em></strong> value, rather than an actual <em><strong>tic</strong></em> handle. It seems that whichever value is passed will always result in the same result, but this result is uncorrelated to any possible event (start of Matlab, midnight etc.). It even works without any prior <em><strong>tic</strong></em> invocation! I would love to hear your thoughts on what you think this strange <em><strong>toc</strong></em> result might represent:</p><blockquote><pre><span style="color: #000000;">&gt;&gt; toc(uint64(1))</span>
Elapsed time is 72825.947547 seconds.
<span style="color: #000000;">&gt;&gt; toc(uint64(1234))</span>
Elapsed time is 72826.538296 seconds.</pre></blockquote><p><pre> </pre>Related posts:<ol><li><a
href='http://undocumentedmatlab.com/blog/more-undocumented-timing-features/' rel='bookmark' title='More undocumented timing features'>More undocumented timing features</a> <small>There are several undocumented ways in Matlab to get CPU and clock data...</small></li><li><a
href='http://undocumentedmatlab.com/blog/ismembc-undocumented-helper-function/' rel='bookmark' title='ismembc &#8211; undocumented helper function'>ismembc &#8211; undocumented helper function</a> <small>Matlab has several undocumented internal helper functions that can be useful on their own in some cases. This post presents the ismembc function....</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><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></ol></p><pre> </pre>]]></content:encoded> <wfw:commentRss>http://undocumentedmatlab.com/blog/tic-toc-undocumented-option/feed/</wfw:commentRss> <slash:comments>11</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-02-04 03:42:24 -->

<!-- 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.109s
Header info:
X-Pingback:         http://undocumentedmatlab.com/blog/xmlrpc.php
Set-Cookie:         wpgb_visit_last_php-default=1328352143; expires=Sun, 03-Feb-2013 10:42:23 GMT; path=/
Content-Type:       text/xml; charset=UTF-8
Last-Modified:      Sat, 04 Feb 2012 10:42:24 GMT
Vary:               Accept-Encoding, Cookie
Expires:            Sat, 04 Feb 2012 11:42:24 GMT
Pragma:             public
Cache-Control:      public, must-revalidate, proxy-revalidate
Etag:               2be016a8742933fce1c2a61ec9aed9a7
Content-Encoding:   gzip
-->
