# How to speed up this following code

I am doing my research about scheduling by using Matlab. I am a new Matlab user and I have a problem with time execution of my following code:

clc
clear all
A=8;
B=12;
C=10;
ProcessTime= [ 11 11 11 11 4 4 4 4 4 4 4 2 ]; %Converting Matrice

RandomMatriceA=zeros(A,B,C);
RandomMatriceB=zeros(A,B,C);
SumRandomMatriceB=zeros(1,A,C);
ConvertMatriceA=zeros(A,B,C);
ProcessRandomMatriceA=zeros(A,B,C);
StartProcess=zeros(A,B,C);
EndProcess=zeros(A,B,C);
StartPost=zeros(A,B,C);

for ii=1:C;
for x=1:2:A-1;
%make first random matrice
[vals RandomMatriceA(x,:,ii)]=sort(rand(1,B),2); %batasan tidak boleh satu kelompok melakukan lebih dari satu aktivitas dalam satu waktu
done=false;
while ~done,
%make row of second random matrice
NewRowRandomMatriceB=randi([0 2],1,B);
%Make sure sum all element per row in RandomMatriceB <=11
done=sum(NewRowRandomMatriceB)<12;
end
RandomMatriceB(x,:,ii)=NewRowRandomMatriceB;
%After making RandomMatriceA and RandomMatriceB, then Make New
%Matrice
%To know varible of new matrice which is result of combining
%RandomMatriceA,RandomMatriceB and ProcessTime
for y=1:B,
ConvertMatriceA(x,y,ii)=ProcessTime(RandomMatriceA(x,y,ii));%FirstVarible:Consecutive The Number of value in all element of RandomMatriceA
end
ProcessRandomMatriceA(x,:,ii)=ProcessTime(RandomMatriceA(x,:,ii))+RandomMatriceB(x,:,ii);
EndProcess(x,:,ii)=cumsum(ProcessRandomMatriceA(x,:,ii),2);%secondVaribale:to know in which column The Consecutive value RandomMatrice will be end
StartProcess(x,:,ii)=EndProcess(x,:,ii)-(ProcessTime(RandomMatriceA(x,:,ii))-1);%ThirdVariable:to know in which column The Consecutive Value will be start
for yy=1:B;
N=RandomMatriceA(x,yy,ii);
StartPost(x,N,ii)=StartProcess(x,yy,ii);
end
end
for h=2:2:A;
doneA=false;
while ~doneA,
[vals RandomMatriceA(h,:,ii)]=sort(rand(1,B),2);
doneB=false;
while ~doneB,
NewRowRandomMatriceB=randi([0 2],1,B);
doneB= sum(NewRowRandomMatriceB)<12;
end
RandomMatriceB(h,:,ii)=NewRowRandomMatriceB;
for y=1:B;
ConvertMatriceA(h,y,ii)=ProcessTime(RandomMatriceA(h,y,ii));
end
ProcessRandomMatriceA(h,:,ii)=ProcessTime(RandomMatriceA(h,:,ii))+RandomMatriceB(h,:,ii);
EndProcess(h,:,ii)=cumsum(ProcessRandomMatriceA(h,:,ii),2);%secondVaribale:to know in which column The Consecutive value RandomMatrice will be end
StartProcess(h,:,ii)=EndProcess(h,:,ii)-(ProcessTime(RandomMatriceA(h,:,ii))-1);%ThirdVariable:to know in which column The Consecutive Value will be start
for z=1:B;
N=RandomMatriceA(h,z,ii);
StartPost(h,N,ii)=StartProcess(h,z,ii);
end
doneA =all(ismember(StartPost(h,1:4,ii),StartPost(h-1,1:4,ii)));
end
%arrange value in column 1 until 4 in order that it will have same
%value
StartPost(h,1:4,ii)=StartPost(h-1,1:4,ii);
end
end


After using the profile viewer, I know that my problem is in this following code. This part takes too much execution time.

....
end
doneA =all(ismember(StartPost(h,1:4,ii),StartPost(h-1,1:4,ii)));
end
%arrange value in column 1 until 4 in order that it will have same
%value
StartPost(h,1:4,ii)=StartPost(h-1,1:4,ii);
end


How can I improve it and make it run faster?

-
@Anna lear: thanks for help to edit my question. –  engineering2008 Sep 5 '12 at 19:16

## 1 Answer

My best guess given the information: You are doing some expensive ismember operations. You can try to get rid of them by tracking Done in a different way.

Perhaps you can use a more efficent way to do it in the same location, or perhaps you need to move it inside the loop and do it in a very cheap way there.

-
+1 - ismember is one of the slowest things you can do in matlab. ismember inside a loop is a real performance killer. Anyway all this code is not properly vectorized. –  avip Nov 16 '12 at 17:45