<?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>Ken Johnson &#8211; Undocumented Matlab</title>
	<atom:link href="https://undocumentedmatlab.com/articles/tag/ken-johnson/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Wed, 31 Aug 2016 17:00:44 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>
	<item>
		<title>Zero-testing performance</title>
		<link>https://undocumentedmatlab.com/articles/zero-testing-performance?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=zero-testing-performance</link>
					<comments>https://undocumentedmatlab.com/articles/zero-testing-performance#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 31 Aug 2016 17:00:44 +0000</pubDate>
				<category><![CDATA[Guest bloggers]]></category>
		<category><![CDATA[Low risk of breaking in future versions]]></category>
		<category><![CDATA[Ken Johnson]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Pure Matlab]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6622</guid>

					<description><![CDATA[<p>Subtle changes in the way that we test for zero/non-zero entries in Matlab can have a significant performance impact. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/zero-testing-performance">Zero-testing performance</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/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="https://undocumentedmatlab.com/articles/performance-accessing-handle-properties" rel="bookmark" title="Performance: accessing handle properties">Performance: accessing handle properties </a> <small>Handle object property access (get/set) performance can be significantly improved using dot-notation. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/improving-fwrite-performance" rel="bookmark" title="Improving fwrite performance">Improving fwrite performance </a> <small>Standard file writing performance can be improved in Matlab in surprising ways. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/convolution-performance" rel="bookmark" title="Convolution performance">Convolution performance </a> <small>Matlab's internal implementation of convolution can often be sped up significantly using the Convolution Theorem. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p><i>I would like to introduce guest blogger <a href="https://www.linkedin.com/in/ken-johnson-b7329610" rel="nofollow" target="_blank">Ken Johnson</a>, a MATLAB Connections partner specializing in <a href="http://www.mathworks.com/products/connections/product_detail/product_35871.html" rel="nofollow" target="_blank">electromagnetic optics simulation</a>. Today Ken will explore some performance subtleties of zero testing in Matlab.</i><br />
I often have a need to efficiently test a large Matlab array for any nonzero elements, e.g.</p>
<pre lang="matlab">
>> a = zeros(1e4);
>> tic, b = any(a(:)~=0); toc
Elapsed time is 0.126118 seconds.
</pre>
<p>Simple enough. In this case, when a is all-zero, the internal search algorithm has no choice but to inspect every element of the array to determine whether it contains any nonzeros. In the more typical case where a contains many nonzeros you would expect the search to terminate almost immediately, as soon as it finds the first nonzero. But that&#8217;s not how it works:</p>
<pre lang="matlab">
>> a = round(rand(1e4));
>> tic, b = any(a(:)~=0); toc
Elapsed time is 0.063404 seconds.
</pre>
<p>There is significant runtime overhead in constructing the logical array &#8220;a(:)~=0&#8221;, although the &#8220;any(&#8230;)&#8221; operation apparently terminates at the first true value it finds.<br />
The overhead can be eliminated by taking advantage of the fact that numeric values may be used as logicals in Matlab, with zero implicitly representing false and nonzero representing true.  Repeating the above test without “~=0”, we get a huge runtime improvement:</p>
<pre lang="matlab">
>> a = round(rand(1e4));
>> tic, b = any(a(:)); toc
Elapsed time is 0.000026 seconds.
</pre>
<p><span id="more-6622"></span><br />
However, there is no runtime benefit when a is all-zero:</p>
<pre lang="matlab">
>> a = zeros(1e4);
>> tic, b = any(a(:)); toc
Elapsed time is 0.125120 seconds.
</pre>
<p><i>(I do not quite understand this. There should be some runtime benefit from bypassing the logical array construction.)</i></p>
<h3 id="nan">NaN values</h3>
<p>There is also another catch: The above efficiency trick does not work when a contains NaN values (if you consider NaN to be nonzero), e.g.</p>
<pre lang="matlab">
>> any([0,nan])
ans =
     0
</pre>
<p>The <i><b>any</b></i> function ignores entries that are NaN, meaning it treats NaNs as zero-equivalent. This is inconsistent with the behavior of the inequality operator:</p>
<pre lang="matlab">
>> any([0,nan]~=0)
ans =
     1
</pre>
<p>To avoid this problem, an explicit <i><b>isnan</b></i> test is needed. Efficiency is not impaired when a contains many nonzeros, but there is a 2x efficiency loss when a is all-zero:</p>
<pre lang="matlab">
>> a = round(rand(1e4));
>> tic, b = any(a(:)) || any(isnan(a(:))); toc
Elapsed time is 0.000027 seconds.
>> a = zeros(1e4);
>> tic, b = any(a(:)) || any(isnan(a(:))); toc
Elapsed time is 0.256604 seconds.
</pre>
<p>For testing all-nonzero the NaN problem does not occur:</p>
<pre lang="matlab">
>> all([1 nan])
ans =
     1
</pre>
<p>In this context NaN is treated as nonzero and the all-nonzero test is straightforward:</p>
<pre lang="matlab">
>> a = round(rand(1e4));
>> tic, b = all(a(:)); toc
Elapsed time is 0.000029 seconds.
</pre>
<p>For testing any-zero and all-zero, use the complements of the above tests:</p>
<pre lang="matlab">
>> b = ~any(a(:)) || any(isnan(a(:)));  % all zero?
>> b = ~all(a(:));  % any zero?
</pre>
<h3 id="find">Efficient <i>find</i></h3>
<p>The <i><b>find</b></i> operation can also be optimized by bypassing construction of a logical temporary array, e.g.</p>
<pre lang="matlab">
>> a = round(rand(1e4));
>> tic, b = find(a(:)~=0, 1); toc
Elapsed time is 0.065697 seconds.
>> tic, b = find(a(:), 1); toc
Elapsed time is 0.000029 seconds.
</pre>
<p>There is no problem with NaNs in this case; the <i><b>find</b></i> function treats NaN as nonzero, e.g.</p>
<pre lang="matlab">
>> find([0,nan,1], 1)
ans =
     2
</pre>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/zero-testing-performance">Zero-testing performance</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/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="https://undocumentedmatlab.com/articles/performance-accessing-handle-properties" rel="bookmark" title="Performance: accessing handle properties">Performance: accessing handle properties </a> <small>Handle object property access (get/set) performance can be significantly improved using dot-notation. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/improving-fwrite-performance" rel="bookmark" title="Improving fwrite performance">Improving fwrite performance </a> <small>Standard file writing performance can be improved in Matlab in surprising ways. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/convolution-performance" rel="bookmark" title="Convolution performance">Convolution performance </a> <small>Matlab's internal implementation of convolution can often be sped up significantly using the Convolution Theorem. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/zero-testing-performance/feed</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
	</channel>
</rss>
