insertN speedup

Petter Risholm <[email protected]> Sun, 29 Feb 2004 22:47:32 +0100 (MET)
Newsgroups gmane.comp.gnu.octave.sources
Message-ID <[email protected]>
hi,

I've eliminated N-d indexing  in Array<T>::insertN which has resulted in a
considerable speedup for [] concatenation of n-d array objects.

see attached patch.

ChangeLog:

2004-02-29  Petter Risholm <[email protected]>

	* Array.cc (Array<T>::insertN):
	Eliminate N-d indexing.

petter risholm
insert.patch (text/plain, 2.3 KB)
Index: Array.cc
===================================================================
RCS file: /cvs/octave/liboctave/Array.cc,v
retrieving revision 1.106
diff -c -p -r1.106 Array.cc
*** Array.cc	2004/02/23 15:35:00	1.106
--- Array.cc	2004/02/29 21:39:24
*************** template <class T>
*** 966,971 ****
--- 966,973 ----
  Array<T>&
  Array<T>::insertN (const Array<T>& a, int r, int c)
  {
+   dim_vector dv = dims ();
+ 
    dim_vector a_dv = a.dims ();
  
    int n = a_dv.length ();
*************** Array<T>::insertN (const Array<T>& a, in
*** 979,985 ****
  
        for (int i = 0; i < n; i++)
  	{
! 	  if (a_ra_idx (i) < 0 || (a_ra_idx (i) + a_dv (i)) > dimensions (i))
  	    {
  	      (*current_liboctave_error_handler)
  		("Array<T>::insert: range error for insert");
--- 981,987 ----
  
        for (int i = 0; i < n; i++)
  	{
! 	  if (a_ra_idx(i) < 0 || (a_ra_idx(i) + a_dv(i)) > dv(i))
  	    {
  	      (*current_liboctave_error_handler)
  		("Array<T>::insert: range error for insert");
*************** Array<T>::insertN (const Array<T>& a, in
*** 987,1007 ****
  	    }
  	}
  
-       a_ra_idx.elem (0) = 0;
-       a_ra_idx.elem (1) = 0;
- 
        int n_elt = a.numel ();
  
        for (int i = 0; i < n_elt; i++)
  	{
! 	  Array<int> ra_idx = a_ra_idx;
! 
! 	  ra_idx.elem (0) = a_ra_idx (0) + r;
! 	  ra_idx.elem (1) = a_ra_idx (1) + c;
  
! 	  elem (ra_idx) = a.elem (a_ra_idx);
! 
! 	  increment_index (a_ra_idx, a_dv);
  	}
      }
    else
--- 989,1017 ----
  	    }
  	}
  
        int n_elt = a.numel ();
+       
+       const T *a_data = a.data ();   
+    
+       int iidx = 0;
+ 	  
+       int a_rows = a_dv(0);
+ 
+       int this_rows = dv(0);
+ 	  
+       int numel_page = a_dv(0) * a_dv(1);	  
  
+       int count_pages = 0;
+ 	  
        for (int i = 0; i < n_elt; i++)
  	{
! 	  if (i != 0 && i % a_rows == 0)
! 	    iidx += (this_rows - a_rows);	      
! 	  
! 	  if (i % numel_page == 0)
! 	    iidx = c * dv(0) + r + dv(0) * dv(1) * count_pages++;
  
! 	  elem (iidx++) = a_data[i];
  	}
      }
    else
*************** Array<T>::insertN (const Array<T>& a, in
*** 1009,1014 ****
--- 1019,1025 ----
        ("Array<T>::insert: invalid indexing operation");
  
    return *this;
+ 
  }
  
  template <class T>