changeset 10373:1f11fabfa349

improve performance of unique for sparse arrays
author John W. Eaton <jwe@octave.org>
date Sun, 28 Feb 2010 12:32:16 -0500
parents 634e182d34e4
children 950c23c26f87
files scripts/ChangeLog scripts/set/unique.m
diffstat 2 files changed, 27 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Sun Feb 28 11:41:03 2010 -0500
+++ b/scripts/ChangeLog	Sun Feb 28 12:32:16 2010 -0500
@@ -1,3 +1,10 @@
+2010-02-28  John W. Eaton  <jwe@octave.org>
+
+	* set/unique.m: If the argument is sparse and we are not
+	operating on rows and we don't need indices, convert nonzero
+	elements to a full matrix and work on that instead, converting
+	back to sparse when done.
+
 2010-02-28  John W. Eaton  <jwe@octave.org>
 
 	* set/unique.m: Return 0x1 arrays for empty arrays with some
--- a/scripts/set/unique.m	Sun Feb 28 11:41:03 2010 -0500
+++ b/scripts/set/unique.m	Sun Feb 28 12:32:16 2010 -0500
@@ -74,6 +74,26 @@
     optrows = 0;
   endif
 
+  ## FIXME -- the operations
+  ##
+  ##   match = (y(1:n-1) == y(2:n));
+  ##   y(idx) = [];
+  ##
+  ## are very slow on sparse matrices.  Until they are fixed to be as
+  ## fast as for full matrices, operate on the nonzero elements of the
+  ## sparse array as long as we are not operating on rows.
+
+  ## FIXME -- unique is called when PKG_ADD files are parsed, but
+  ## issparse is not yet available because it is coming from a .oct
+  ## file?!?
+
+  if (exist ("issparse"))
+    if (issparse (x) && ! optrows && nargout <= 1)
+      y = unique ([0; (full (nonzeros (x)))], varargin{:});
+      return;
+    endif
+  endif
+
   if (optrows)
     n = size (x, 1);
     dim = 1;
@@ -132,7 +152,6 @@
     i(idx) = [];
   endif
 
-
 endfunction
 
 %!assert(unique([1 1 2; 1 2 1; 1 1 2]),[1;2])