- Undocumented Matlab - https://undocumentedmatlab.com -

Zero-testing performance

Posted By Yair Altman On August 31, 2016 | 5 Comments

I would like to introduce guest blogger Ken Johnson [1], a MATLAB Connections partner specializing in electromagnetic optics simulation [2]. Today Ken will explore some performance subtleties of zero testing in Matlab.
I often have a need to efficiently test a large Matlab array for any nonzero elements, e.g.

>> a = zeros(1e4);
>> tic, b = any(a(:)~=0); toc
Elapsed time is 0.126118 seconds.

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’s not how it works:

>> a = round(rand(1e4));
>> tic, b = any(a(:)~=0); toc
Elapsed time is 0.063404 seconds.

There is significant runtime overhead in constructing the logical array “a(:)~=0”, although the “any(…)” operation apparently terminates at the first true value it finds.
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:

>> a = round(rand(1e4));
>> tic, b = any(a(:)); toc
Elapsed time is 0.000026 seconds.


However, there is no runtime benefit when a is all-zero:

>> a = zeros(1e4);
>> tic, b = any(a(:)); toc
Elapsed time is 0.125120 seconds.

(I do not quite understand this. There should be some runtime benefit from bypassing the logical array construction.)

NaN values

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.

>> any([0,nan])
ans =
     0

The any function ignores entries that are NaN, meaning it treats NaNs as zero-equivalent. This is inconsistent with the behavior of the inequality operator:

>> any([0,nan]~=0)
ans =
     1

To avoid this problem, an explicit isnan test is needed. Efficiency is not impaired when a contains many nonzeros, but there is a 2x efficiency loss when a is all-zero:

>> 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.

For testing all-nonzero the NaN problem does not occur:

>> all([1 nan])
ans =
     1

In this context NaN is treated as nonzero and the all-nonzero test is straightforward:

>> a = round(rand(1e4));
>> tic, b = all(a(:)); toc
Elapsed time is 0.000029 seconds.

For testing any-zero and all-zero, use the complements of the above tests:

>> b = ~any(a(:)) || any(isnan(a(:)));  % all zero?
>> b = ~all(a(:));  % any zero?

Efficient find

The find operation can also be optimized by bypassing construction of a logical temporary array, e.g.

>> 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.

There is no problem with NaNs in this case; the find function treats NaN as nonzero, e.g.

>> find([0,nan,1], 1)
ans =
     2

Categories: Guest bloggers, Low risk of breaking in future versions


5 Comments (Open | Close)

5 Comments To "Zero-testing performance"

#1 Comment By Andy Stamps On August 31, 2016 @ 22:46

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 ‘timeit’ 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.

a = zeros(1e4);
f = @()all(all(a));
timeit(f)

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.

#2 Comment By Daniel E. Shub On September 1, 2016 @ 17:01

You need to be careful with using nans in “logical” tests. While one might expect that since all(nan) is true, that and(nan, nan) would also be true. While the behavior varies with MATLAB version, and possibly OS, with R2015b on Linux:

>> and(nan, nan)
NaN's cannot be converted to logicals.

and you can cause MATLAB (again R2015b on Linux) to die a fiery death with:

bsxfun(@(a,b)and(a, b), true(10, 1), [true(10, 1), nan(10, 1)])

#3 Comment By Yair Altman On September 6, 2016 @ 19:08

@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 (@(a,b)and(a,b)) and not with the standard function-handle variant (@and).

#4 Comment By ly On September 2, 2016 @ 19:46

test this:

tic, a = zeros(1e4); toc
tic, b = any(a(:)); toc
tic, c = repelem(0,1e4,1e4); toc
tic, d = any(c(:)); toc

It seems that ZEROS does not initialize an array (to 0) immediately.

#5 Comment By Jan Simon On October 5, 2016 @ 17:58

I’ve published a C-Mex function to check if two array have any equal numbers:
[9]
This avoids the creation of the temporary logical array and returns early if any element is found.

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.

This is faster for finding NaNs and INFs also:

tic, b = any(isnan(a(:))); toc
% Elapsed time is 0.489283 seconds.
tic, b = anyEq(a, NaN); toc
% Elapsed time is 0.192315 seconds.

Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/articles/zero-testing-performance

URLs in this post:

[1] Ken Johnson: https://www.linkedin.com/in/ken-johnson-b7329610

[2] electromagnetic optics simulation: http://www.mathworks.com/products/connections/product_detail/product_35871.html

[3] Performance: scatter vs. line : https://undocumentedmatlab.com/articles/performance-scatter-vs-line

[4] Performance: accessing handle properties : https://undocumentedmatlab.com/articles/performance-accessing-handle-properties

[5] Improving fwrite performance : https://undocumentedmatlab.com/articles/improving-fwrite-performance

[6] Convolution performance : https://undocumentedmatlab.com/articles/convolution-performance

[7] rmfield performance : https://undocumentedmatlab.com/articles/rmfield-performance

[8] uicontextmenu performance : https://undocumentedmatlab.com/articles/uicontextmenu-performance

[9] : http://www.mathworks.com/matlabcentral/fileexchange/26867-anyeq

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.