<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
	
	>
<channel>
	<title>
	Comments on: Performance: accessing handle properties	</title>
	<atom:link href="https://undocumentedmatlab.com/articles/performance-accessing-handle-properties/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com/articles/performance-accessing-handle-properties?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=performance-accessing-handle-properties</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Tue, 26 Nov 2013 22:03:36 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.3</generator>
	<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/performance-accessing-handle-properties#comment-299387</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Tue, 26 Nov 2013 22:03:36 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4403#comment-299387</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/performance-accessing-handle-properties#comment-297945&quot;&gt;Xiaohu&lt;/a&gt;.

@Andrew - you may indeed be correct, but when we use a.B=c notation, the setB(a,c) function is still getting invoked if you declare it, which seems to indicate that the opposite is the case. But then it would not explain why a.B would be faster than calling setB() directly. I don&#039;t have a good answer to explain the apparent contradiction.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/performance-accessing-handle-properties#comment-297945">Xiaohu</a>.</p>
<p>@Andrew &#8211; you may indeed be correct, but when we use a.B=c notation, the setB(a,c) function is still getting invoked if you declare it, which seems to indicate that the opposite is the case. But then it would not explain why a.B would be faster than calling setB() directly. I don&#8217;t have a good answer to explain the apparent contradiction.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Andrew		</title>
		<link>https://undocumentedmatlab.com/articles/performance-accessing-handle-properties#comment-299384</link>

		<dc:creator><![CDATA[Andrew]]></dc:creator>
		<pubDate>Tue, 26 Nov 2013 21:47:44 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4403#comment-299384</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/performance-accessing-handle-properties#comment-297945&quot;&gt;Xiaohu&lt;/a&gt;.

This is not really the case of two different ways of invoking a method. We&#039;re setting or getting a property. Properties can either be directly specified/obtained using set/get methods (allowing for things like validation) or via &quot;dot&quot; notation. The latter is much simpler and probably why this is faster. Indeed, trying to get nonexistent properties using the various methods shows that they return different error messages. When setting nonexistent properties, the set method returns an error, but the &quot;dot&quot; notation methods obviously do not. This additional overhead of validation may be what is slowing down get/set over repeated calls.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/performance-accessing-handle-properties#comment-297945">Xiaohu</a>.</p>
<p>This is not really the case of two different ways of invoking a method. We&#8217;re setting or getting a property. Properties can either be directly specified/obtained using set/get methods (allowing for things like validation) or via &#8220;dot&#8221; notation. The latter is much simpler and probably why this is faster. Indeed, trying to get nonexistent properties using the various methods shows that they return different error messages. When setting nonexistent properties, the set method returns an error, but the &#8220;dot&#8221; notation methods obviously do not. This additional overhead of validation may be what is slowing down get/set over repeated calls.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Andrew		</title>
		<link>https://undocumentedmatlab.com/articles/performance-accessing-handle-properties#comment-299379</link>

		<dc:creator><![CDATA[Andrew]]></dc:creator>
		<pubDate>Tue, 26 Nov 2013 21:29:54 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4403#comment-299379</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/performance-accessing-handle-properties#comment-299199&quot;&gt;Bjorn&lt;/a&gt;.

... the blog apparently ate my n-dash and the rest of my post. In any case, similarly,  I wonder which method would be fastest for getting/setting multiple properties of the same handle when using cell arrays, i.e., as in &lt;a href=&quot;http://stackoverflow.com/a/20165547/2278029&quot; rel=&quot;nofollow&quot;&gt;this StackOverflow/Matlab answer&lt;/a&gt; of mine.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/performance-accessing-handle-properties#comment-299199">Bjorn</a>.</p>
<p>&#8230; the blog apparently ate my n-dash and the rest of my post. In any case, similarly,  I wonder which method would be fastest for getting/setting multiple properties of the same handle when using cell arrays, i.e., as in <a href="http://stackoverflow.com/a/20165547/2278029" rel="nofollow">this StackOverflow/Matlab answer</a> of mine.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Andrew		</title>
		<link>https://undocumentedmatlab.com/articles/performance-accessing-handle-properties#comment-299375</link>

		<dc:creator><![CDATA[Andrew]]></dc:creator>
		<pubDate>Tue, 26 Nov 2013 21:21:49 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4403#comment-299375</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/performance-accessing-handle-properties#comment-299199&quot;&gt;Bjorn&lt;/a&gt;.

Interesting ]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/performance-accessing-handle-properties#comment-299199">Bjorn</a>.</p>
<p>Interesting </p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Bjorn		</title>
		<link>https://undocumentedmatlab.com/articles/performance-accessing-handle-properties#comment-299199</link>

		<dc:creator><![CDATA[Bjorn]]></dc:creator>
		<pubDate>Tue, 26 Nov 2013 13:10:02 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4403#comment-299199</guid>

					<description><![CDATA[This seems to be a good approach when resetting the same handle over and over again. Not quite the case when updating many different graphical objects.

&lt;pre lang=&quot;matlab&quot;&gt;
%Setup many axes in one figure
tmp = figure;
Fig = zeros(1,10000);
for idx=1:10000
    Fig(idx) = axes(&#039;parent&#039;,tmp);
end

% Using standard set() function
tic, for idx=1:10000, set(Fig(idx),&#039;tag&#039;,&#039;testing&#039;); end, toc % Elapsed time is 0.163880 seconds

% Using the HG handle() wrapper within the loop
tic, for idx=1:10000, hFig = handle(Fig(idx)); set(hFig,&#039;tag&#039;,&#039;testing&#039;); end, toc % Elapsed time is 0.375456 seconds

% Using the HG handle() wrapper outside of the loop
tic
hFig = handle(Fig);
for idx=1:10000, set(hFig(idx),&#039;tag&#039;,&#039;testing&#039;); end, 
toc % Elapsed time is 0.241068 seconds

% Using dot notation with handle() wrapper within the loop
tic
for idx=1:10000, hFig = handle(Fig(idx)); hFig.tag = &#039;testing&#039;; end
toc % Elapsed time is 0.290306 seconds

% Using dot notation with handle() wrapper outside of the loop
tic
hFig = handle(Fig);
for idx=1:10000, hFig(idx).tag = &#039;testing&#039;; end
toc % Elapsed time is 3.585026 seconds
&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>This seems to be a good approach when resetting the same handle over and over again. Not quite the case when updating many different graphical objects.</p>
<pre lang="matlab">
%Setup many axes in one figure
tmp = figure;
Fig = zeros(1,10000);
for idx=1:10000
    Fig(idx) = axes('parent',tmp);
end

% Using standard set() function
tic, for idx=1:10000, set(Fig(idx),'tag','testing'); end, toc % Elapsed time is 0.163880 seconds

% Using the HG handle() wrapper within the loop
tic, for idx=1:10000, hFig = handle(Fig(idx)); set(hFig,'tag','testing'); end, toc % Elapsed time is 0.375456 seconds

% Using the HG handle() wrapper outside of the loop
tic
hFig = handle(Fig);
for idx=1:10000, set(hFig(idx),'tag','testing'); end, 
toc % Elapsed time is 0.241068 seconds

% Using dot notation with handle() wrapper within the loop
tic
for idx=1:10000, hFig = handle(Fig(idx)); hFig.tag = 'testing'; end
toc % Elapsed time is 0.290306 seconds

% Using dot notation with handle() wrapper outside of the loop
tic
hFig = handle(Fig);
for idx=1:10000, hFig(idx).tag = 'testing'; end
toc % Elapsed time is 3.585026 seconds
</pre>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/performance-accessing-handle-properties#comment-298130</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Sat, 23 Nov 2013 15:45:03 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4403#comment-298130</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/performance-accessing-handle-properties#comment-297945&quot;&gt;Xiaohu&lt;/a&gt;.

@Xiaohu - I do not understand why this is, but it appears to be consistent. If anyone has an explanation, I&#039;d be happy to learn.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/performance-accessing-handle-properties#comment-297945">Xiaohu</a>.</p>
<p>@Xiaohu &#8211; I do not understand why this is, but it appears to be consistent. If anyone has an explanation, I&#8217;d be happy to learn.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Xiaohu		</title>
		<link>https://undocumentedmatlab.com/articles/performance-accessing-handle-properties#comment-297945</link>

		<dc:creator><![CDATA[Xiaohu]]></dc:creator>
		<pubDate>Fri, 22 Nov 2013 23:18:53 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4403#comment-297945</guid>

					<description><![CDATA[Nice tip (as always)!
It is however surprising to me that using dot notation is actually faster. As you probably know, method invocation using function notation is typically faster in Matlab, see:
&lt;a target=&quot;_blank&quot; href=&quot;http://blogs.mathworks.com/loren/2012/03/26/considering-performance-in-object-oriented-matlab-code/#11&quot; rel=&quot;nofollow&quot;&gt;http://blogs.mathworks.com/loren/2012/03/26/considering-performance-in-object-oriented-matlab-code/#11&lt;/a&gt;
&lt;a target=&quot;_blank&quot; href=&quot;http://stackoverflow.com/questions/1693429/is-matlab-oop-slow-or-am-i-doing-something-wrong&quot; rel=&quot;nofollow&quot;&gt;http://stackoverflow.com/questions/1693429/is-matlab-oop-slow-or-am-i-doing-something-wrong&lt;/a&gt;
Any insight on this?]]></description>
			<content:encoded><![CDATA[<p>Nice tip (as always)!<br />
It is however surprising to me that using dot notation is actually faster. As you probably know, method invocation using function notation is typically faster in Matlab, see:<br />
<a target="_blank" href="http://blogs.mathworks.com/loren/2012/03/26/considering-performance-in-object-oriented-matlab-code/#11" rel="nofollow">http://blogs.mathworks.com/loren/2012/03/26/considering-performance-in-object-oriented-matlab-code/#11</a><br />
<a target="_blank" href="http://stackoverflow.com/questions/1693429/is-matlab-oop-slow-or-am-i-doing-something-wrong" rel="nofollow">http://stackoverflow.com/questions/1693429/is-matlab-oop-slow-or-am-i-doing-something-wrong</a><br />
Any insight on this?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Malcolm Lidierth		</title>
		<link>https://undocumentedmatlab.com/articles/performance-accessing-handle-properties#comment-297816</link>

		<dc:creator><![CDATA[Malcolm Lidierth]]></dc:creator>
		<pubDate>Fri, 22 Nov 2013 12:40:07 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4403#comment-297816</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/performance-accessing-handle-properties#comment-297636&quot;&gt;Malcolm Lidierth&lt;/a&gt;.

PPS. Adding the code to a script file and using run allows the compiled code to be used each time. Then we see

&lt;pre lang=&#039;matlab&#039;&gt;
&gt;&gt; GShell.load(&#039;/Users/ML/blogtest.groovy&#039;)  % Load and compile once
ans =
     1

&gt;&gt; tic;GShell.run();toc
Elapsed time is 0.060009 seconds.
&gt;&gt; tic;GShell.run();toc
Elapsed time is 0.019712 seconds.
&gt;&gt; tic;GShell.run();toc
Elapsed time is 0.003378 seconds.
&gt;&gt; tic;GShell.run();toc
Elapsed time is 0.003129 seconds.
&gt;&gt; tic;GShell.run();toc
Elapsed time is 0.003126 seconds.
&gt;&gt; tic;GShell.run();toc
Elapsed time is 0.003531 seconds.
&gt;&gt; tic;GShell.run();toc
Elapsed time is 0.003365 seconds.
&gt;&gt; tic;GShell.run();toc
Elapsed time is 0.003394 seconds.
&lt;/pre&gt;

The performance improvements are down to HotSpot I guess.

As this is a JButton, the code should really be run on the EDT.
Using &lt;code&gt;GShell.runEDT&lt;/code&gt; - which uses &lt;i&gt;invokeAndWait&lt;/i&gt; - performance returns to ~0.03s.
Using &lt;code&gt;GShell.runLater&lt;/code&gt; - which uses &lt;i&gt;invokeLater&lt;/i&gt; - performance improves another 10x (the MATLAB thread is not blocked as control returns as soon as the code is posted to the EDT).

&lt;i&gt;invokeAndWait&lt;/i&gt; cannot be called if already on the EDT (e.g. if &lt;i&gt;&lt;b&gt;javaObjectEDT&lt;/b&gt;&lt;/i&gt; has been used on the button). &lt;code&gt;GShell&lt;/code&gt; solves that with &lt;code&gt;GShell.runQuery&lt;/code&gt; which uses &lt;code&gt;GShell.runEDT&lt;/code&gt; if it can, and &lt;code&gt;GShell.runLater&lt;/code&gt; otherwise.


Note that GShell is independent of the rest of the Waterloo code - in fact it&#039;s wrapped in an RStudio  project folder and can be used from MATLAB, R, Python, SciLab, JRuby etc. It can be used to run any Java/Groovy code and control threading e.g. to create a background SwingWorker thread or work on the JavaFX platform thread.

GShell contains little or no novel code - it&#039;s just giving easy access to a Groovy shell instance from other scripting environments.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/performance-accessing-handle-properties#comment-297636">Malcolm Lidierth</a>.</p>
<p>PPS. Adding the code to a script file and using run allows the compiled code to be used each time. Then we see</p>
<pre lang='matlab'>
>> GShell.load('/Users/ML/blogtest.groovy')  % Load and compile once
ans =
     1

>> tic;GShell.run();toc
Elapsed time is 0.060009 seconds.
>> tic;GShell.run();toc
Elapsed time is 0.019712 seconds.
>> tic;GShell.run();toc
Elapsed time is 0.003378 seconds.
>> tic;GShell.run();toc
Elapsed time is 0.003129 seconds.
>> tic;GShell.run();toc
Elapsed time is 0.003126 seconds.
>> tic;GShell.run();toc
Elapsed time is 0.003531 seconds.
>> tic;GShell.run();toc
Elapsed time is 0.003365 seconds.
>> tic;GShell.run();toc
Elapsed time is 0.003394 seconds.
</pre>
<p>The performance improvements are down to HotSpot I guess.</p>
<p>As this is a JButton, the code should really be run on the EDT.<br />
Using <code>GShell.runEDT</code> &#8211; which uses <i>invokeAndWait</i> &#8211; performance returns to ~0.03s.<br />
Using <code>GShell.runLater</code> &#8211; which uses <i>invokeLater</i> &#8211; performance improves another 10x (the MATLAB thread is not blocked as control returns as soon as the code is posted to the EDT).</p>
<p><i>invokeAndWait</i> cannot be called if already on the EDT (e.g. if <i><b>javaObjectEDT</b></i> has been used on the button). <code>GShell</code> solves that with <code>GShell.runQuery</code> which uses <code>GShell.runEDT</code> if it can, and <code>GShell.runLater</code> otherwise.</p>
<p>Note that GShell is independent of the rest of the Waterloo code &#8211; in fact it&#8217;s wrapped in an RStudio  project folder and can be used from MATLAB, R, Python, SciLab, JRuby etc. It can be used to run any Java/Groovy code and control threading e.g. to create a background SwingWorker thread or work on the JavaFX platform thread.</p>
<p>GShell contains little or no novel code &#8211; it&#8217;s just giving easy access to a Groovy shell instance from other scripting environments.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Malcolm Lidierth		</title>
		<link>https://undocumentedmatlab.com/articles/performance-accessing-handle-properties#comment-297636</link>

		<dc:creator><![CDATA[Malcolm Lidierth]]></dc:creator>
		<pubDate>Fri, 22 Nov 2013 02:16:27 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4403#comment-297636</guid>

					<description><![CDATA[While this may not be a real application test case note that using GShell (which uses a Groovy shell) from Waterloo in MATLAB gives: 

&lt;pre lang=&quot;matlab&quot;&gt;
tic;
GShell.setVariable(&#039;button&#039;, jhb);
GShell.eval(&#039;for (def idx=0; idx &lt; 10000; idx++) { button.setText(&quot;testing&quot;)&#039;); 
toc;
&lt;/pre&gt;

Elapsed time is 0.019272 seconds.

A ~10x improvement on the best of the above on my machine (after GShell warm up).

Further info at &lt;a href=&quot;http://waterloo.sourceforge.net/groovy/GShell.html&quot; target=&quot;_blank&quot; rel=&quot;nofollow&quot;&gt;http://waterloo.sourceforge.net/groovy/GShell.html&lt;/a&gt;

Horses-for-courses: expect JVM languages to outperform MATLAB on JVM operations ; and MATLAB to (substantially) outperform the JVM when working with natives.&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>While this may not be a real application test case note that using GShell (which uses a Groovy shell) from Waterloo in MATLAB gives: </p>
<pre lang="matlab">
tic;
GShell.setVariable('button', jhb);
GShell.eval('for (def idx=0; idx < 10000; idx++) { button.setText("testing")'); 
toc;
</pre>
<p>Elapsed time is 0.019272 seconds.</p>
<p>A ~10x improvement on the best of the above on my machine (after GShell warm up).</p>
<p>Further info at <a href="http://waterloo.sourceforge.net/groovy/GShell.html" target="_blank" rel="nofollow">http://waterloo.sourceforge.net/groovy/GShell.html</a></p>
<p>Horses-for-courses: expect JVM languages to outperform MATLAB on JVM operations ; and MATLAB to (substantially) outperform the JVM when working with natives.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Dan		</title>
		<link>https://undocumentedmatlab.com/articles/performance-accessing-handle-properties#comment-297548</link>

		<dc:creator><![CDATA[Dan]]></dc:creator>
		<pubDate>Thu, 21 Nov 2013 18:33:44 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4403#comment-297548</guid>

					<description><![CDATA[Yair, I noticed something interesting.  It appears that you pay the time penalty the first time you access any property within a given scope.  Try this: create a handle object and pass it into a function (you can pass it in within a cell array, at a minimum).  The first time you access any property within the function you pay the time penalty, but all subsequent calls are fast.  BUT: you pay that penalty every time you call the function...  Try profiling this:
&lt;pre lang=&quot;matlab&quot;&gt;
function SpeedTests
   fig = figure;
   for i = 1:4
      hObj(i) = subplot(2,2,i);
      handleObj{i} = handle(hObj(i));
      handleObjs2(i) = handle(hObj(i));
   end
   for itest = 1:1000
      subfun(handleObj,hObj,handleObjs2);
   end
   return
end

function subfun(handleObj,hObj,handleObjs2)
   for i = 1:4
      pos0 = handleObjs2(i).Position;
      pos1 = handleObj{i}.Position;
      pos2 = get(hObj(i),&#039;Position&#039;);
      pos3 = handleObj{i}.Position;
      XLIMS = handleObj{i}.XLim;
   end
end
&lt;/pre&gt;

Any thoughts?]]></description>
			<content:encoded><![CDATA[<p>Yair, I noticed something interesting.  It appears that you pay the time penalty the first time you access any property within a given scope.  Try this: create a handle object and pass it into a function (you can pass it in within a cell array, at a minimum).  The first time you access any property within the function you pay the time penalty, but all subsequent calls are fast.  BUT: you pay that penalty every time you call the function&#8230;  Try profiling this:</p>
<pre lang="matlab">
function SpeedTests
   fig = figure;
   for i = 1:4
      hObj(i) = subplot(2,2,i);
      handleObj{i} = handle(hObj(i));
      handleObjs2(i) = handle(hObj(i));
   end
   for itest = 1:1000
      subfun(handleObj,hObj,handleObjs2);
   end
   return
end

function subfun(handleObj,hObj,handleObjs2)
   for i = 1:4
      pos0 = handleObjs2(i).Position;
      pos1 = handleObj{i}.Position;
      pos2 = get(hObj(i),'Position');
      pos3 = handleObj{i}.Position;
      XLIMS = handleObj{i}.XLim;
   end
end
</pre>
<p>Any thoughts?</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
