bugGNU Octave - Bugs: bug #53605, (minor) nonzeros.m can be made...

 
 

bug #53605: (minor) nonzeros.m can be made twice as fast, as follows:

Submitter:  deego <deego>
Submitted:  Tue 10 Apr 2018 06:43:09 PM UTC
   
 
Category:  Performance Severity:  3 - Normal
Priority:  5 - Normal Item Group:  None
Status:  Fixed Assigned to:  None
Originator Name:  deego Open/Closed:  * Closed
Release:  * 4.2.2 Operating System:  * GNU/Linux
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Tue 10 Apr 2018 10:43:50 PM UTC, comment #2: 

It's a little more complicated than that.  nonzeros(), according to the documentation, is meant for sparse matrices.  If you try it on sparse matrices then using the third argument from find() approach is 10X faster than using indexing.  But for full matrices, the approache you recommended is faster.  The solution, obviously, is just to check the incoming matrix type and use the fastest algorithm.


  if (issparse (A))
    [~, ~, v] = find (A);
    v = v(:);
  else
    v = A(find (A));
    v = v(:);
  endif


I checked this in on the development branch of Octave (http://hg.savannah.gnu.org/hgweb/octave/rev/ff830f8e61ac).

Rik <rik5>
Group administrator
Tue 10 Apr 2018 06:43:55 PM UTC, comment #1: 

Typo: The latter calculation was 0.1s, not 0.2s.

deego <deego>
Tue 10 Apr 2018 06:43:09 PM UTC, original submission:  

It uses the [~,~,b] = find(a) invocation, whereas find doesn't bother to check if the first two argouts are ignored, and does the calculation any way.


Either find() could be fixed, or nonzeros should switch to the a=find(a) invocation.
                                                                                             



a = randi([0 1], 1e3, 1e3); ## random matrix of 0s and 1s.

tic;
for ii=1:10;
  [~,~,b] = find(a); ## method used by nonzeros.
  ## Or, b = nonzeros(a);
endfor
toc ## 0.2s

tic;
for ii=1:10;
  b = a(find(a));
endfor
toc ## 0.2s


deego <deego>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by deego (Submitted the item)
  • -email is unavailable- added by deego
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 3 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2018-04-10 rik5 StatusNone Fixed
        Open/ClosedOpen Closed
    2018-04-10 deego Carbon-Copy- Added -email is unavailable-

    Back to the top

    Powered by Savane 3.13-02a9.
    Corresponding source code