2 views (last 30 days)

hi, my project running time make me crazy.

I write a script with only 3 " for " but when I click on run button it takes 166 second to run that.

I try it in other computers and Ihave same problem so I realize that the problem is in my script.

with same inputs my friend write another script and we both get same output but his script ran in 3 seconds and my script runs in 166 seconds. (why?) (we have same laptops)

my laptop's specs :

cpu = corei7 5500U

ram = 8 GB

here is my code please tell me where is the problem that I cant get answer in few seconds.

clc

clear

tic;

Input = [ 5 12 0.02

4 20 0.1

6 50 0.01

4 76 0.02

3 100 0.04

4 155 0.04

3 197 0.05

1 350 0.08

2 400 0.12];

n=Input(:,1);

cap=Input(:,2);

FOR=Input(:,3);

COPT=zeros;

copt1=zeros;

Prob=zeros;

%copt 1

for i=0:n(1)

copt1(i+1,1) = cap(1)*i;

copt1(i+1,2) = (nchoosek(n(1),i))*(FOR(1)^(i))*((1-FOR(1))^(n(1)-i));

end

l=1;

for i=1:size(copt1,1)

COPT(l,1)=copt1(i,1);

COPT(l,2)=copt1(i,2);

l=l+1;

end

%problem is starts from here

for i=2:size(cap,1)

a=size(COPT,1);

for j=1:n(i)

for k=1:a

COPT(l,1)=cap(i)*j + COPT(k,1);

l=l+1;

end

end

end

toc;

Shubham Gupta
on 7 Nov 2019

Edited: Shubham Gupta
on 7 Nov 2019

I changed one simple thing, it was working more efficiently for me:

clc

clear;

tic;

Input = [ 5 12 0.02

4 20 0.1

6 50 0.01

4 76 0.02

3 100 0.04

4 155 0.04

3 197 0.05

1 350 0.08

2 400 0.12];

n=Input(:,1);

cap=Input(:,2);

FOR=Input(:,3);

COPT=zeros;

copt1=zeros;

Prob=zeros;

%copt 1

for i=0:n(1)

copt1(i+1,1) = cap(1)*i;

copt1(i+1,2) = (nchoosek(n(1),i))*(FOR(1)^(i))*((1-FOR(1))^(n(1)-i));

end

COPT =copt1(:,1); % I removed second loop and in place made COPT a column vector

l=size(COPT,1)+1;

for i=2:size(cap,1)

a=size(COPT,1);

for j=1:n(i)

for k=1:a

COPT(l,1)=cap(i)*j + COPT(k,1);

l=l+1;

end

end

end

COPT(:,2) = zeros(size(COPT,2));

COPT(1:6,2) = copt1(:,2);

toc;

I believe this is something to do with not prelocating the size of an array, MATLAB uses more memory and bigger the dimesion of the matrix more is the time taken to change the size of it. Let me know if this works for you .

Shubham Gupta
on 7 Nov 2019

I am glad it worked.

As I have mentioned in the answer, you can prelocate the matrix that will be subjected to change it's size with time. But that might not always be possible, so you can reduce the dimesion of the matrix or reduce number of loops that are involved. There are no hard rules that you have to follow, it's a continuous learning process.

The major problem was when you defined COPT an array with 2 columns but in the 3rd loop you were only updating 1st column, but since you already defined COPT with 2 columns it was forced to update 2nd column with zeros. This process increased the processing time a lot. Because now the matrix was being updated 2 columns at once for each loop, whose dimesions were not fixed in the first place.

Let me know if you have further doubts.

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/489607-problem-in-running-time-of-my-project#comment_764465

⋮## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/489607-problem-in-running-time-of-my-project#comment_764465

## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/489607-problem-in-running-time-of-my-project#comment_764554

⋮## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/489607-problem-in-running-time-of-my-project#comment_764554

Sign in to comment.