<?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: Speeding-up builtin Matlab functions &#8211; part 2	</title>
	<atom:link href="https://undocumentedmatlab.com/articles/speeding-up-builtin-matlab-functions-part-2/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com/articles/speeding-up-builtin-matlab-functions-part-2?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=speeding-up-builtin-matlab-functions-part-2</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Mon, 25 Jun 2018 18:19:10 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>
	<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/speeding-up-builtin-matlab-functions-part-2#comment-431032</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Mon, 25 Jun 2018 18:19:10 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7533#comment-431032</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/speeding-up-builtin-matlab-functions-part-2#comment-431031&quot;&gt;Michelle Hirsch&lt;/a&gt;.

@Michelle - thank you for the update. 

Please note the &lt;a href=&quot;http://undocumentedmatlab.com/blog/speeding-up-builtin-matlab-functions-part-2#comment-426525&quot; target=&quot;_blank&quot; rel=&quot;nofollow&quot;&gt;extra tip here&lt;/a&gt;, in case the developers missed it, since it was not included in the main post body.

Also, please consider adding corresponding vectorized functions for maxdrawup/emaxdrawup - Whereas draw-ups are used less frequently than draw-downs, they too are widely used. If you wish, I&#039;ll be happy to send you my version of maxdrawup to serve as a baseline for your developers.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/speeding-up-builtin-matlab-functions-part-2#comment-431031">Michelle Hirsch</a>.</p>
<p>@Michelle &#8211; thank you for the update. </p>
<p>Please note the <a href="http://undocumentedmatlab.com/blog/speeding-up-builtin-matlab-functions-part-2#comment-426525" target="_blank" rel="nofollow">extra tip here</a>, in case the developers missed it, since it was not included in the main post body.</p>
<p>Also, please consider adding corresponding vectorized functions for maxdrawup/emaxdrawup &#8211; Whereas draw-ups are used less frequently than draw-downs, they too are widely used. If you wish, I&#8217;ll be happy to send you my version of maxdrawup to serve as a baseline for your developers.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Michelle Hirsch		</title>
		<link>https://undocumentedmatlab.com/articles/speeding-up-builtin-matlab-functions-part-2#comment-431031</link>

		<dc:creator><![CDATA[Michelle Hirsch]]></dc:creator>
		<pubDate>Mon, 25 Jun 2018 18:06:41 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7533#comment-431031</guid>

					<description><![CDATA[Yair - just a quick note to let you know that the development team investigated these suggestions for maxdrawdown and emaxdrawdown and the changes look promising. Assuming everything pans out as expected, we should see the performance improvement in R2019a. (Usual disclaimers about the reality of software development apply).

Thanks for bringing the issue to our attention.]]></description>
			<content:encoded><![CDATA[<p>Yair &#8211; just a quick note to let you know that the development team investigated these suggestions for maxdrawdown and emaxdrawdown and the changes look promising. Assuming everything pans out as expected, we should see the performance improvement in R2019a. (Usual disclaimers about the reality of software development apply).</p>
<p>Thanks for bringing the issue to our attention.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Marshall		</title>
		<link>https://undocumentedmatlab.com/articles/speeding-up-builtin-matlab-functions-part-2#comment-426577</link>

		<dc:creator><![CDATA[Marshall]]></dc:creator>
		<pubDate>Mon, 21 May 2018 01:47:26 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7533#comment-426577</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/speeding-up-builtin-matlab-functions-part-2#comment-426401&quot;&gt;Marshall&lt;/a&gt;.

Thanks Yair--I wasn&#039;t trying to say &quot;look you could have sped this up!&quot;, I was instead asking if there was a better way of performing the loop I made in the second example, using vectorization instead of a for loop, which is the un-Matlab way to do it. I was mainly disappointed that Matlab wasn&#039;t optimizing away vectorization of X==9 in conjunction with find(X==9,1).]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/speeding-up-builtin-matlab-functions-part-2#comment-426401">Marshall</a>.</p>
<p>Thanks Yair&#8211;I wasn&#8217;t trying to say &#8220;look you could have sped this up!&#8221;, I was instead asking if there was a better way of performing the loop I made in the second example, using vectorization instead of a for loop, which is the un-Matlab way to do it. I was mainly disappointed that Matlab wasn&#8217;t optimizing away vectorization of X==9 in conjunction with find(X==9,1).</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/speeding-up-builtin-matlab-functions-part-2#comment-426525</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Sun, 20 May 2018 17:53:13 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7533#comment-426525</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/speeding-up-builtin-matlab-functions-part-2#comment-426401&quot;&gt;Marshall&lt;/a&gt;.

@Marshal - I accept that additional speedup is possible for the line you noted, in cases involving 1e9 data elements (as in your example). As I noted in my post, the new function is ~40 times faster than Matlab&#039;s built-in function, to the point where additional speedups might not have been very cost-effective, especially since my use-case involved up to a few million (not billions of) data elements. In such cases, shaving off a few dozen millisecs did not seem to be worth the extra coding time and risk.

If you are interested in extra speedup, an additional tip would be to enclose the entire section that computes &lt;code&gt;MaxIndex1&lt;/code&gt;, &lt;code&gt;MaxIndex2&lt;/code&gt; and &lt;code&gt;MaxDDIndex&lt;/code&gt; with a check that the &lt;code&gt;MaxDDIndex&lt;/code&gt; output arg is in fact requested - in many use cases it is not, and in such cases there is no need to compute these indexes:
&lt;pre lang=&quot;matlab&quot; highlight=&quot;1,6&quot;&gt;
if nargout &gt; 1
   MaxIndex2 = find(MaxDD==DD,1,&#039;last&#039;);
   MaxIndex1 = MaxIndexes(find(MaxIndexes&lt;=MaxIndex2,1,&#039;last&#039;));
   MaxDDIndex(1,:) = MaxIndex1;
   MaxDDIndex(2,:) = MaxIndex2;
end
&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/speeding-up-builtin-matlab-functions-part-2#comment-426401">Marshall</a>.</p>
<p>@Marshal &#8211; I accept that additional speedup is possible for the line you noted, in cases involving 1e9 data elements (as in your example). As I noted in my post, the new function is ~40 times faster than Matlab&#8217;s built-in function, to the point where additional speedups might not have been very cost-effective, especially since my use-case involved up to a few million (not billions of) data elements. In such cases, shaving off a few dozen millisecs did not seem to be worth the extra coding time and risk.</p>
<p>If you are interested in extra speedup, an additional tip would be to enclose the entire section that computes <code>MaxIndex1</code>, <code>MaxIndex2</code> and <code>MaxDDIndex</code> with a check that the <code>MaxDDIndex</code> output arg is in fact requested &#8211; in many use cases it is not, and in such cases there is no need to compute these indexes:</p>
<pre lang="matlab" highlight="1,6">
if nargout > 1
   MaxIndex2 = find(MaxDD==DD,1,'last');
   MaxIndex1 = MaxIndexes(find(MaxIndexes<=MaxIndex2,1,'last'));
   MaxDDIndex(1,:) = MaxIndex1;
   MaxDDIndex(2,:) = MaxIndex2;
end
</pre>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Marshall		</title>
		<link>https://undocumentedmatlab.com/articles/speeding-up-builtin-matlab-functions-part-2#comment-426401</link>

		<dc:creator><![CDATA[Marshall]]></dc:creator>
		<pubDate>Sat, 19 May 2018 17:58:55 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7533#comment-426401</guid>

					<description><![CDATA[Hi Yair,

One of the lines in your code
&lt;pre lang=&quot;matlab&quot;&gt;
MaxIndex2 = find(MaxDD==DD,1,&#039;last&#039;);
&lt;/pre&gt;

is one that is incredibly common but is undoubtedly inefficient because the &lt;code&gt;==&lt;/code&gt; compares every element, despite the fact that our call to &lt;code&gt;find&lt;/code&gt; requests that we stop. As an example, the following two are roughly comparable in time:

&lt;pre lang=&quot;matlab&quot;&gt;
% compare using vectorization
X = zeros(1e9,1); X(5e8) = 9;
tic; find(X==9,1); t_vec = toc;

% compare using a for-loop
tic;
for i = 1:length(X)
    if(X(i)==9), break; end
end
t_iter = toc;

fprintf(&#039;Vectorized: %2.2f\n&#039;,t_vec);
fprintf(&#039;Iterative: %2.2f\n&#039;,t_iter);

Vectorized: 3.94
Iterative: 3.58
&lt;/pre&gt;

However, in the following scenario, the issue becomes obvious:

&lt;pre lang=&quot;matlab&quot;&gt;
X = zeros(1e9,1); X(1) = 9;
tic; find(X==9,1); t_vec = toc;

% compare using a for-loop
tic;
for i = 1:length(X)
    if(X(i)==9), break; end
end
t_iter = toc;

fprintf(&#039;Vectorized: %2.2f\n&#039;,t_vec);
fprintf(&#039;Iterative: %2.2f\n&#039;,t_iter);

Vectorized: 2.19
Iterative: 0.00
&lt;/pre&gt;

From this we can deduce that the call to &lt;code&gt;find()&lt;/code&gt; in the first example takes about 2 seconds, with the other 2 seconds due to the comparison &lt;code&gt;X==9&lt;/code&gt;. However, in the second example, the entirety of &lt;code&gt;X&lt;/code&gt; is still compared to 9 before the find is called

A smart compiler would optimize away the common code pattern of &lt;code&gt;find(X==y)&lt;/code&gt;, but it appears Matlab&#039;s JIT doesn&#039;t. Is there a more obvious idiomatic Matlab method of avoiding performing a full comparison when it&#039;s not necessary?]]></description>
			<content:encoded><![CDATA[<p>Hi Yair,</p>
<p>One of the lines in your code</p>
<pre lang="matlab">
MaxIndex2 = find(MaxDD==DD,1,'last');
</pre>
<p>is one that is incredibly common but is undoubtedly inefficient because the <code>==</code> compares every element, despite the fact that our call to <code>find</code> requests that we stop. As an example, the following two are roughly comparable in time:</p>
<pre lang="matlab">
% compare using vectorization
X = zeros(1e9,1); X(5e8) = 9;
tic; find(X==9,1); t_vec = toc;

% compare using a for-loop
tic;
for i = 1:length(X)
    if(X(i)==9), break; end
end
t_iter = toc;

fprintf('Vectorized: %2.2f\n',t_vec);
fprintf('Iterative: %2.2f\n',t_iter);

Vectorized: 3.94
Iterative: 3.58
</pre>
<p>However, in the following scenario, the issue becomes obvious:</p>
<pre lang="matlab">
X = zeros(1e9,1); X(1) = 9;
tic; find(X==9,1); t_vec = toc;

% compare using a for-loop
tic;
for i = 1:length(X)
    if(X(i)==9), break; end
end
t_iter = toc;

fprintf('Vectorized: %2.2f\n',t_vec);
fprintf('Iterative: %2.2f\n',t_iter);

Vectorized: 2.19
Iterative: 0.00
</pre>
<p>From this we can deduce that the call to <code>find()</code> in the first example takes about 2 seconds, with the other 2 seconds due to the comparison <code>X==9</code>. However, in the second example, the entirety of <code>X</code> is still compared to 9 before the find is called</p>
<p>A smart compiler would optimize away the common code pattern of <code>find(X==y)</code>, but it appears Matlab&#8217;s JIT doesn&#8217;t. Is there a more obvious idiomatic Matlab method of avoiding performing a full comparison when it&#8217;s not necessary?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: John		</title>
		<link>https://undocumentedmatlab.com/articles/speeding-up-builtin-matlab-functions-part-2#comment-426161</link>

		<dc:creator><![CDATA[John]]></dc:creator>
		<pubDate>Fri, 18 May 2018 00:19:53 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7533#comment-426161</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/speeding-up-builtin-matlab-functions-part-2#comment-426147&quot;&gt;John&lt;/a&gt;.

Yair, thanks for the quick reply and explanation]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/speeding-up-builtin-matlab-functions-part-2#comment-426147">John</a>.</p>
<p>Yair, thanks for the quick reply and explanation</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/speeding-up-builtin-matlab-functions-part-2#comment-426151</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Thu, 17 May 2018 23:01:27 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7533#comment-426151</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/speeding-up-builtin-matlab-functions-part-2#comment-426147&quot;&gt;John&lt;/a&gt;.

@John - the reason is that MaxIndexes is compared to another indices array, MaxIndex2, below. For this comparison we need numeric indices, not a logical array.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/speeding-up-builtin-matlab-functions-part-2#comment-426147">John</a>.</p>
<p>@John &#8211; the reason is that MaxIndexes is compared to another indices array, MaxIndex2, below. For this comparison we need numeric indices, not a logical array.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: John		</title>
		<link>https://undocumentedmatlab.com/articles/speeding-up-builtin-matlab-functions-part-2#comment-426147</link>

		<dc:creator><![CDATA[John]]></dc:creator>
		<pubDate>Thu, 17 May 2018 22:42:27 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7533#comment-426147</guid>

					<description><![CDATA[Hi Yair

just wondering if the find function is really required
for example is: &lt;code&gt;MaxIndexes = MaxData==Data;&lt;/code&gt; equivalent to &lt;code&gt;MaxIndexes = find(MaxData==Data);&lt;/code&gt;

best regards]]></description>
			<content:encoded><![CDATA[<p>Hi Yair</p>
<p>just wondering if the find function is really required<br />
for example is: <code>MaxIndexes = MaxData==Data;</code> equivalent to <code>MaxIndexes = find(MaxData==Data);</code></p>
<p>best regards</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
