How can we reduce the execution time of this whole code?

I want to minimize the "execution time" of the code given in the attachment. You can run the m file "main".

2 Comments

In myfunction, Why are you pre-allocating an empty matrix?
K = length(u); %Constant4
c=zeros(M*N, length(u)-K);
ce=zeros(M*N, length(u)-K);
%Modify it as
c=zeros(M*N,K);
ce=zeros(M*N,K);
And club the two for loop together -
for g= 1:K
c(:,g)=kron(a(:,g),f(:,g));
end
for g= 1:K
ce(:,g)=kron(ae(:,g),fe(:,g));
end
into
for g = i %i is already defined in the code
c(:,g)=kron(a(:,g),f(:,g));
ce(:,g)=kron(ae(:,g),fe(:,g));
end
Additionally, pre-allocate Sol and Fitness in fp1
After these changes, the code runs in 2.5674 secs in i3-5th gen, 8gb RAM, which imo is fast enough.
Thanks a lot dear Dyuman Joshi for your kind response. In myfunction, the pre-allocation of an empty matrix speeds upt the process? With this its time is reduced and without this, it take more time. Yes, you are right, the two for-loop can br clubbed togather. But how to pre-allocate Sol and Fitness in fp1?

Sign in to comment.

 Accepted Answer

I am aware that pre-allocation speeds up the process, but pre-allocating an empty matrix doesn't reserve any memory for the operation and it is slower than pre-allocating with a zeros matrix. See below -
Modify the pre-allocation as I have suggested above.
v=1e6;
y=zeros(v,0);
z=zeros(v,1);
whos y z
Name Size Bytes Class Attributes y 1000000x0 0 double z 1000000x1 8000000 double
tic
for ix=1:v
y(ix)=i;
end
toc
Elapsed time is 0.151647 seconds.
tic
for ix=1:v
z(ix)=i;
end
toc
Elapsed time is 0.025546 seconds.
"But how to pre-allocate Sol and Fitness in fp1?"
It's clear from the code
%for i=1:psize
% Sol(i,:)=Lb+(Ub-Lb).*rand(1,d);
% Fitness(i)=fun(Sol(i,:));%--------------------(1)
%end
Fitness has psize elements, so size is (1xpsize, as the output of the function myfunction is a scalar value)
and Sol has psize rows and d columns.
Pre-allocate zero matrices accordingly.

3 Comments

Thanks a lot dear Dyuman Joshi for your kind response. I checked the time in both the cases i.e., without preallocation by commenting these lines:
a=zeros(M,K);
f=zeros(N,K);
c=zeros(M*N, K);
ae=zeros(M,K);
fe=zeros(N,K);
ce=zeros(M*N, K);
and with pre-allocation i.e., uncommenting the above lines again. I found that the time with pre-allocation is lower than without preo-allocation. You can do it your self. Further, I did preallocation inside fpa1 as below:
Sol=zeros(n,dim);
Fitness=zeros(N_iter,1);
But it was giving error. Then I did the changes as below:
Sol=zeros(n+1,dim);
Fitness=zeros(N_iter,1);
then it ran but all the fitness and solutions were 0s which is not possible. So if you do it for me, it will be nice of you. Regards,
"I found that the time with pre-allocation is lower than without pre-allocation."
I am well aware of this. But that was not my point.
Nevertheless, what you have done now is better than what you did before.
How did you arrive at n+1 and N_iter?
Ugh. I specifically mentioned what to use for pre-allocation of Sol and Fitness -
"Fitness has psize elements, so size is (1xpsize, as the output of the function myfunction is a scalar value)
and Sol has psize rows and d columns.
Pre-allocate zero matrices accordingly. "
Use this -
Sol=zeros(psize,dim);
Fitness=zeros(psize,1);
Thanks a lot dear Dyuman Joshi for your kind response. Indeed now it works. But still the time is not reduced so much. Can you convert the for-loops inside fpa1 to vectorized form because this may redue the time?

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!