<?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: Zero-testing performance	</title>
	<atom:link href="https://undocumentedmatlab.com/articles/zero-testing-performance/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com/articles/zero-testing-performance?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=zero-testing-performance</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Wed, 05 Oct 2016 14:58:57 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>
	<item>
		<title>
		By: Jan Simon		</title>
		<link>https://undocumentedmatlab.com/articles/zero-testing-performance#comment-390010</link>

		<dc:creator><![CDATA[Jan Simon]]></dc:creator>
		<pubDate>Wed, 05 Oct 2016 14:58:57 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6622#comment-390010</guid>

					<description><![CDATA[I&#039;ve published a C-Mex function to check if two array have any equal numbers:
http://www.mathworks.com/matlabcentral/fileexchange/26867-anyeq
This avoids the creation of the temporary logical array and returns early if any element is found.

&lt;pre lang=&quot;matlab&quot;&gt;
a = rand(1e4)+1;
tic, b = any(a(:)==0); toc
% Elapsed time is 0.490473 seconds.
tic, b = ~all(a); toc
% Elapsed time is 0.255272 seconds.
tic, b = anyEq(a, 0); toc
% Elapsed time is 0.214196 seconds.
&lt;/pre&gt;

This is faster for finding NaNs and INFs also:
&lt;pre lang=&quot;matlab&quot;&gt;
tic, b = any(isnan(a(:))); toc
% Elapsed time is 0.489283 seconds.
tic, b = anyEq(a, NaN); toc
% Elapsed time is 0.192315 seconds.
&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve published a C-Mex function to check if two array have any equal numbers:<br />
<a href="http://www.mathworks.com/matlabcentral/fileexchange/26867-anyeq" rel="nofollow ugc">http://www.mathworks.com/matlabcentral/fileexchange/26867-anyeq</a><br />
This avoids the creation of the temporary logical array and returns early if any element is found.</p>
<pre lang="matlab">
a = rand(1e4)+1;
tic, b = any(a(:)==0); toc
% Elapsed time is 0.490473 seconds.
tic, b = ~all(a); toc
% Elapsed time is 0.255272 seconds.
tic, b = anyEq(a, 0); toc
% Elapsed time is 0.214196 seconds.
</pre>
<p>This is faster for finding NaNs and INFs also:</p>
<pre lang="matlab">
tic, b = any(isnan(a(:))); toc
% Elapsed time is 0.489283 seconds.
tic, b = anyEq(a, NaN); toc
% Elapsed time is 0.192315 seconds.
</pre>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/zero-testing-performance#comment-387513</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Tue, 06 Sep 2016 16:08:15 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6622#comment-387513</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/zero-testing-performance#comment-387125&quot;&gt;Daniel E. Shub&lt;/a&gt;.

@Daniel - the fiery death that you reported is due to an internal Matlab bug (reported).

Oddly, the crash only happens with the anonymous-function variant (&lt;code&gt;@(a,b)and(a,b)&lt;/code&gt;) and not with the standard function-handle variant (&lt;code&gt;@and&lt;/code&gt;).]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/zero-testing-performance#comment-387125">Daniel E. Shub</a>.</p>
<p>@Daniel &#8211; the fiery death that you reported is due to an internal Matlab bug (reported).</p>
<p>Oddly, the crash only happens with the anonymous-function variant (<code>@(a,b)and(a,b)</code>) and not with the standard function-handle variant (<code>@and</code>).</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: ly		</title>
		<link>https://undocumentedmatlab.com/articles/zero-testing-performance#comment-387216</link>

		<dc:creator><![CDATA[ly]]></dc:creator>
		<pubDate>Fri, 02 Sep 2016 16:46:29 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6622#comment-387216</guid>

					<description><![CDATA[test this:
&lt;pre lang=&quot;matlab&quot;&gt;
tic, a = zeros(1e4); toc
tic, b = any(a(:)); toc
tic, c = repelem(0,1e4,1e4); toc
tic, d = any(c(:)); toc
&lt;/pre&gt;
It seems that ZEROS does not initialize an array (to 0) immediately.]]></description>
			<content:encoded><![CDATA[<p>test this:</p>
<pre lang="matlab">
tic, a = zeros(1e4); toc
tic, b = any(a(:)); toc
tic, c = repelem(0,1e4,1e4); toc
tic, d = any(c(:)); toc
</pre>
<p>It seems that ZEROS does not initialize an array (to 0) immediately.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Daniel E. Shub		</title>
		<link>https://undocumentedmatlab.com/articles/zero-testing-performance#comment-387125</link>

		<dc:creator><![CDATA[Daniel E. Shub]]></dc:creator>
		<pubDate>Thu, 01 Sep 2016 14:01:11 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6622#comment-387125</guid>

					<description><![CDATA[You need to be careful with using nans in &quot;logical&quot; tests. While one might expect that since &lt;code&gt;all(nan)&lt;/code&gt; is true, that &lt;code&gt;and(nan, nan)&lt;/code&gt; would also be true. While the behavior varies with MATLAB version, and possibly OS, with R2015b on Linux:

&lt;pre lang=&quot;matlab&quot;&gt;
&gt;&gt; and(nan, nan)
NaN&#039;s cannot be converted to logicals.
&lt;/pre&gt;

and you can cause MATLAB (again R2015b on Linux) to die a fiery death with:
&lt;pre lang=&quot;matlab&quot;&gt;bsxfun(@(a,b)and(a, b), true(10, 1), [true(10, 1), nan(10, 1)])&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>You need to be careful with using nans in &#8220;logical&#8221; tests. While one might expect that since <code>all(nan)</code> is true, that <code>and(nan, nan)</code> would also be true. While the behavior varies with MATLAB version, and possibly OS, with R2015b on Linux:</p>
<pre lang="matlab">
>> and(nan, nan)
NaN's cannot be converted to logicals.
</pre>
<p>and you can cause MATLAB (again R2015b on Linux) to die a fiery death with:</p>
<pre lang="matlab">bsxfun(@(a,b)and(a, b), true(10, 1), [true(10, 1), nan(10, 1)])</pre>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Andy Stamps		</title>
		<link>https://undocumentedmatlab.com/articles/zero-testing-performance#comment-387073</link>

		<dc:creator><![CDATA[Andy Stamps]]></dc:creator>
		<pubDate>Wed, 31 Aug 2016 19:46:11 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6622#comment-387073</guid>

					<description><![CDATA[Regarding your comment about the runtime benefit from bypassing the logical array construction, I have to say that I do see the improvement on my machine (0.02-0.03 second).  Given the behavior of the JIT-compiler, I think it also makes sense to perform repeated runs and average the results, particularly for these tests that take relatively little time.  The &#039;timeit&#039; function simplifies this process.

I will also suggest that there is overhead in constructing the intermediate array a(:).  In my quick testing on my computer the following seemed to perform better for all zeros or few (i.e. 1) nonzeros.
&lt;pre lang=&quot;matlab&quot;&gt;
a = zeros(1e4);
f = @()all(all(a));
timeit(f)
&lt;/pre&gt;

For the case with many nonzeros described in the post, the all(all(a)) construction was noticeably slower than all(a(:)) form, but still considerably faster than the pathological cases.  As with many performance tuning problems, the appropriate choice really depends on what the typical argument looks like and whether you are trying to improve average performance or the worst-case bound.

Finally, I will note that the all(all(a)) formulation is only appropriate for 2-D arrays, whereas all(a(:)) is generic enough to be used on N-D arrays of any size.]]></description>
			<content:encoded><![CDATA[<p>Regarding your comment about the runtime benefit from bypassing the logical array construction, I have to say that I do see the improvement on my machine (0.02-0.03 second).  Given the behavior of the JIT-compiler, I think it also makes sense to perform repeated runs and average the results, particularly for these tests that take relatively little time.  The &#8216;timeit&#8217; function simplifies this process.</p>
<p>I will also suggest that there is overhead in constructing the intermediate array a(:).  In my quick testing on my computer the following seemed to perform better for all zeros or few (i.e. 1) nonzeros.</p>
<pre lang="matlab">
a = zeros(1e4);
f = @()all(all(a));
timeit(f)
</pre>
<p>For the case with many nonzeros described in the post, the all(all(a)) construction was noticeably slower than all(a(:)) form, but still considerably faster than the pathological cases.  As with many performance tuning problems, the appropriate choice really depends on what the typical argument looks like and whether you are trying to improve average performance or the worst-case bound.</p>
<p>Finally, I will note that the all(all(a)) formulation is only appropriate for 2-D arrays, whereas all(a(:)) is generic enough to be used on N-D arrays of any size.</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
