octave-bug-tracker
[Top][All Lists]
Advanced

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

[Octave-bug-tracker] [bug #59840] repmat and repelem slower than needed,


From: anonymous
Subject: [Octave-bug-tracker] [bug #59840] repmat and repelem slower than needed, faster implimentation suggested
Date: Thu, 7 Jan 2021 20:49:31 -0500 (EST)
User-agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0

URL:
  <https://savannah.gnu.org/bugs/?59840>

                 Summary: repmat and repelem slower than needed, faster
implimentation suggested
                 Project: GNU Octave
            Submitted by: None
            Submitted on: Fri 08 Jan 2021 01:49:29 AM UTC
                Category: Octave Function
                Severity: 3 - Normal
                Priority: 5 - Normal
              Item Group: Performance
                  Status: None
             Assigned to: None
         Originator Name: 
        Originator Email: 
             Open/Closed: Open
                 Release: 6.1.0
         Discussion Lock: Any
        Operating System: Any

    _______________________________________________________

Details:

repmat and repelem are slower than they need to be and in one case
demonstrated they are slower than using loops.

Both repmat and repelem are slower than using kron with numeric matrix input
and repeating in only one direction, repmat is also slower when repeating in
both directions. Could my kron implementation be incorporated into repmat and
repelem where it is faster? Also, why is my loop implementation fastest in the
last example?


function repmatrepelemfaster
q=reshape(1:6,[],2)

repmat(q,2)
repmatmatrixfast(q,2)

repelem(q,2,3)
repelemmatrixfast(q,2,3)

ab=[1 500;
500 1;
50 50]

for k=1:size(ab,1)
  a=ab(k,1);
  b=ab(k,2);
  fprintf('b=%d, c=%d\n',a,b)
  disp('inbuilt repmat');
  tic
  for k=1:1000
    repmat(q,a,b);
  end
  toc
  disp('repmat using kron')
  tic
  for k=1:1000
    repmatmatrixfast(q,a,b);
  end
  toc
  disp('inbuilt repelem')
  tic
  for k=1:1000
    repelem(q,a,b);
  end
  toc
  disp('repelem using kron')
  tic
  for k=1:1000
    repelemmatrixfast(q,a,b);
  end
  toc
end

fprintf('What I was doing when I observed the issue:\nInsert a row into a
column vector repeting the elements of the vector\nThis was used to plot a
slice through a multivariable function.\nSpeed became an issue when I tried to
intergrate the function.\n');

np=500;
xi=rand(1,np);
xo=rand(4,1);
i=2;
disp('Origional attempt')
tic
for l=1:10000
  x=[repmat(xo(1:i-1),1,np);xi;repmat(xo(i:end),1,np)];
end
toc
disp('with faster repmat')
tic
for l=1:10000
  x=[repmatmatrixfast(xo(1:i-1),1,np);xi;repmatmatrixfast(xo(i:end),1,np)];
end
toc
disp('single repmat call')
tic
for l=1:10000
  x=zeros(size(xo,1)+1,size(xi,2));
  x([1:i-1 i+1:end],:)=repmat(xo,1,np);
  x(i,:)=xi;
end
toc
disp('with faster repmat')
tic
for l=1:10000
  x=zeros(size(xo,1)+1,size(xi,2));
  x([1:i-1 i+1:end],:)=repmatmatrixfast(xo,1,np);
  x(i,:)=xi;
end
toc
disp('loop solution')
tic
for l=1:10000
  nv=size(xo,1);
  x=zeros(size(xo,1)+1,size(xi,2));
  x(i,:)=xi;
  for k=[1:i-1]
    x(k,:)=xo(k);
  end
  for k=i+1:nv
    x(k,:)=xo(k-1);
  end
end
toc

end
function r=repmatmatrixfast(a,b,c)
if numel(b)==2
  c=b(2);
  b=b(1);
elseif nargin()==2
  c=b;
end
r=kron(ones(b,1),a);
r=kron(ones(1,c),r);
end
function r=repelemmatrixfast(a,b,c)
r=kron(a,ones(b,1));
r=kron(r,ones(1,c));
end





    _______________________________________________________

Reply to this item at:

  <https://savannah.gnu.org/bugs/?59840>

_______________________________________________
  Message sent via Savannah
  https://savannah.gnu.org/




reply via email to

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