Undocumented Matlab
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT

Zero-testing performance

August 31, 2016 5 Comments

I would like to introduce guest blogger Ken Johnson, a MATLAB Connections partner specializing in electromagnetic optics simulation. 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.

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

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

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

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

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

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

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

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

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

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

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

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

Related posts:

  1. Performance: scatter vs. line – In many circumstances, the line function can generate visually-identical plots as the scatter function, much faster...
  2. Performance: accessing handle properties – Handle object property access (get/set) performance can be significantly improved using dot-notation. ...
  3. Improving fwrite performance – Standard file writing performance can be improved in Matlab in surprising ways. ...
  4. Convolution performance – Matlab's internal implementation of convolution can often be sped up significantly using the Convolution Theorem. ...
  5. rmfield performance – The performance of the builtin rmfield function (as with many other builtin functions) can be improved by simple profiling. ...
  6. uicontextmenu performance – Matlab uicontextmenus are not automatically deleted with their associated objects, leading to leaks and slow-downs. ...
Ken Johnson Performance Pure Matlab
Print Print
« Previous
Next »
5 Responses
  1. Andy Stamps August 31, 2016 at 22:46 Reply

    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)

    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. Daniel E. Shub September 1, 2016 at 17:01 Reply

    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(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)])

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

    • Yair Altman September 6, 2016 at 19:08 Reply

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

  3. ly September 2, 2016 at 19:46 Reply

    test this:

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

    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.

  4. Jan Simon October 5, 2016 at 17:58 Reply

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

    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.

    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.

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

Leave a Reply
HTML tags such as <b> or <i> are accepted.
Wrap code fragments inside <pre lang="matlab"> tags, like this:
<pre lang="matlab">
a = magic(3);
disp(sum(a))
</pre>
I reserve the right to edit/delete comments (read the site policies).
Not all comments will be answered. You can always email me (altmany at gmail) for private consulting.

Click here to cancel reply.

Useful links
  •  Email Yair Altman
  •  Subscribe to new posts (email)
  •  Subscribe to new posts (feed)
  •  Subscribe to new posts (reader)
  •  Subscribe to comments (feed)
 
Accelerating MATLAB Performance book
Recent Posts

Speeding-up builtin Matlab functions – part 3

Improving graphics interactivity

Interesting Matlab puzzle – analysis

Interesting Matlab puzzle

Undocumented plot marker types

Matlab toolstrip – part 9 (popup figures)

Matlab toolstrip – part 8 (galleries)

Matlab toolstrip – part 7 (selection controls)

Matlab toolstrip – part 6 (complex controls)

Matlab toolstrip – part 5 (icons)

Matlab toolstrip – part 4 (control customization)

Reverting axes controls in figure toolbar

Matlab toolstrip – part 3 (basic customization)

Matlab toolstrip – part 2 (ToolGroup App)

Matlab toolstrip – part 1

Categories
  • Desktop (45)
  • Figure window (59)
  • Guest bloggers (65)
  • GUI (165)
  • Handle graphics (84)
  • Hidden property (42)
  • Icons (15)
  • Java (174)
  • Listeners (22)
  • Memory (16)
  • Mex (13)
  • Presumed future risk (394)
    • High risk of breaking in future versions (100)
    • Low risk of breaking in future versions (160)
    • Medium risk of breaking in future versions (136)
  • Public presentation (6)
  • Semi-documented feature (10)
  • Semi-documented function (35)
  • Stock Matlab function (140)
  • Toolbox (10)
  • UI controls (52)
  • Uncategorized (13)
  • Undocumented feature (217)
  • Undocumented function (37)
Tags
ActiveX (6) AppDesigner (9) Callbacks (31) Compiler (10) Desktop (38) Donn Shull (10) Editor (8) Figure (19) FindJObj (27) GUI (141) GUIDE (8) Handle graphics (78) HG2 (34) Hidden property (51) HTML (26) Icons (9) Internal component (39) Java (178) JavaFrame (20) JIDE (19) JMI (8) Listener (17) Malcolm Lidierth (8) MCOS (11) Memory (13) Menubar (9) Mex (14) Optical illusion (11) Performance (78) Profiler (9) Pure Matlab (187) schema (7) schema.class (8) schema.prop (18) Semi-documented feature (6) Semi-documented function (33) Toolbar (14) Toolstrip (13) uicontrol (37) uifigure (8) UIInspect (12) uitools (20) Undocumented feature (187) Undocumented function (37) Undocumented property (20)
Recent Comments
  • JCFL (23 hours 51 minutes ago): I was trying the above trick to change the transparency of contour lines and wondering why not all the lines have changed — then I found your post. Thanks and your post saved me.
  • Patrick Fitzgerald (3 days 3 hours ago): Just a heads up to anyone else digging around for help related to this: if you assign BackrgoundColor to a pushbutton-style uicontrol, it seems to prevent you from...
  • Vasiliy (18 days 23 hours ago): Hi Yair, i’m trying to use the MonthChooserPanel class. com.mathworks.mwswing.MJUtilit ies.initJIDE; handles.jPanel = com.jidesoft.combobox.MonthCho oserPanel;...
  • DM (18 days 23 hours ago): Hi Yair, I’m trying to use the MonthChooserPanel class. com.mathworks.mwswing.MJUtilit ies.initJIDE; handles.jPanel = com.jidesoft.combobox.MonthCho oserPanel; [handles.hPanel,...
  • Yair Altman (22 days 15 hours ago): @Alex – thanks, but keep in mind that new functions will only work on the recent Matlab releases. If your code needs to work on older Matlab releases, you could revert to...
  • Alex Churchill (22 days 15 hours ago): I was looking up how to do this exact task today. I was about to hold my nose and use the internal controllib function, when I happened to chance across a slightly newer...
  • Sebastian Hölz (28 days 21 hours ago): I have not investigated this in detail, but I think one way to go in new versions of Matlab (>2019b) might be to use listeners to suitable figure properties, e.g. fig =...
  • Prathep (29 days 0 hours ago): Hi Yair, Thanks for your introduction on Matlab Toostrip. Is there any way to add Matlab toolstrip for uifigures as you have done already for figure?
  • Josh (35 days 7 hours ago): Dear Yair, Small typo; you wrote >>Move lines up or down – CTRL + ALT + UP or DOWN allows you to move selected lines up or down but it’s actually ALT+SHIFT then UP/DOWN...
  • Yair Altman (42 days 2 hours ago): You can try to increase the Java heap memory size in the Matlab preferences (General => Java Heap Memory). Any changes to the settings will only take effect after restarting...
  • Thomas (42 days 3 hours ago): Hello, thanks for sharing! I currently receive the following memory error message when using savezip with a big object (Matlab R2020b, Windows 10). Error using...
  • Yair Altman (45 days 10 hours ago): Yerlan – either use menuItem1.setEnabled(0) or set(menuItem1,'Enabled',0)
  • Manzn (45 days 13 hours ago): Thank you man! you saved me, when there was no more light 😀
  • Yerlan (46 days 18 hours ago): Hello Mr. Altman, how can I disable menu items in the context menu? E.g. when I am trying to do this: menuItems = jmenu.getSubElements; menuItem1 = menuItems(1);...
  • Yair Altman (47 days 10 hours ago): Thanks for the note Eric – you forgot the crucial call to jTable.setLabelTable(labelTabl e) – I added it into your code snippet above.
Contact us
Undocumented Matlab © 2009 - Yair Altman
Scroll to top