octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Array::insert methods


From: Jaroslav Hajek
Subject: Array::insert methods
Date: Fri, 15 Jan 2010 12:43:39 +0100

hi all,

the following changeset:
http://hg.savannah.gnu.org/hgweb/octave/rev/ed49cef7e005

simplifies the Array<T>::insert methods, making them essentially
wrappers for Array<T>::assign. I was not strictly sure about the
intent of the methods, however.
Here is the documentation I wrote:

  // Insert an array into another at a specified position.
  // If size (a) is [d1 d2 ... dN] and idx is [i1 i2 ... iN],
  // this method is equivalent to
  // x(i1:i1+d1-1, i2:i2+d2-1, ... , iN:iN+dN-1) = a.
  Array<T>& insert (const Array<T>& a, const Array<octave_idx_type>& idx);

  // This is just a special case for idx = [r c 0 ...]
  Array<T>& insert (const Array<T>& a, octave_idx_type r, octave_idx_type c);

John (or anyone), can you confirm this was the true intent?
Since these methods apparently form the heart of concatenation, I also
tried to measure performance impact, using the following benchmark:

1;
function test_4cat (m, n, k)
  a = zeros (m, m, k);
  b = zeros (m, n, k);
  c = zeros (n, m, k);
  d = zeros (n, n, k);
  disp ([m, n, k])
  tic; [a, b]; toc
  tic; [a; c]; toc
  tic; [a, b; c, d]; toc
endfunction

test_4cat (2000, 100, 1)
test_4cat (2000, 1000, 1)
test_4cat (1000, 200, 5)
test_4cat (100, 200, 100)

with recent tip, I get (Core 2 Duo @ 2.83 GHz, g++ -O3 -march=native)
   2000    100      1
Elapsed time is 0.027335 seconds.
Elapsed time is 0.032203 seconds.
Elapsed time is 0.14711 seconds.
   2000   1000      1
Elapsed time is 0.040801 seconds.
Elapsed time is 0.046022 seconds.
Elapsed time is 0.29877 seconds.
   1000    200      5
Elapsed time is 0.04109 seconds.
Elapsed time is 0.046402 seconds.
Elapsed time is 0.30668 seconds.
   100   200   100
Elapsed time is 0.01994 seconds.
Elapsed time is 0.02783 seconds.
Elapsed time is 0.3885 seconds.

and with the new patch, I got

   2000    100      1
Elapsed time is 0.025771 seconds.
Elapsed time is 0.028105 seconds.
Elapsed time is 0.02932 seconds.
   2000   1000      1
Elapsed time is 0.036181 seconds.
Elapsed time is 0.043126 seconds.
Elapsed time is 0.060008 seconds.
   1000    200      5
Elapsed time is 0.037125 seconds.
Elapsed time is 0.043224 seconds.
Elapsed time is 0.047919 seconds.
   100   200   100
Elapsed time is 0.0158 seconds.
Elapsed time is 0.024 seconds.
Elapsed time is 0.06343 seconds.

so, as you can see, depending on case, performance has either been
retained or improved (up to 6x).
I have yet to measure a possible impact for lots of smaller arrays.
It's always cool when better code leads also to better performance :)

enjoy!

-- 
RNDr. Jaroslav Hajek, PhD
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz


reply via email to

[Prev in Thread] Current Thread [Next in Thread]