hi , I write a script and in this script below code take time than other codes in my script.
I need your knowledge to know how can I improve this script. (I know it takes only 0.11 sec to run but in large scale inputs it takes too long and also I want to learn that how can I write a script efficient.)
margin=zeros;
l=1;
C_in= 3405;
for i=1:1824
for j=1:10
temp=COPT(i,3)*COPT_load(j,2);
if temp>d
%this line takes time %from here
margin(l,1)=COPT(i,2)-COPT_load(j,1);
%to here
margin(l,2)=temp;
l=l+1;
end
end
end
So how can I improve my code ???

댓글 수: 2

the cyclist
the cyclist 2019년 11월 13일
Can you upload a *.mat file with the data, so that we can test on your actual data?
f4r3in
f4r3in 2019년 11월 14일
I'm so sorry. this is my project and I cant share it in internet.
but when I click on "run and time" button in MATLAB I understand that these lines take time in my code.
I know you want to help me but for some reasons I cant share it, Sorry.

댓글을 달려면 로그인하십시오.

 채택된 답변

Daniel M
Daniel M 2019년 11월 13일
편집: Daniel M 2019년 11월 13일

2 개 추천

You should try preallocating margin before the loop. You have only set the value to one element, of zero. Since it's not clear how big margin will be at the end of your loops, you should preallocate it to be the maximum size (1824*10 = 18240). Then at the end, only keep the first 1:l (L) rows using
% after your loops
margin = margin(1:l,:);
Let us know if it's still slow after that, and include a mat file so we can test.

댓글 수: 4

the cyclist
the cyclist 2019년 11월 13일
Definitely this. I got a 10x speedup from preallocating.
To better understand why preallocation is important, you could read this documentation.
f4r3in
f4r3in 2019년 11월 14일
thank you very much Daniel M .
I follow yours tips and my code runs faster.
but I have a question : if I want to write a general code that works for any input in that case what should I do because I dont know what is "margin" 's dimention to write that dimention in my pre allocating.
for now I use "size" command in my program but in future I maybe write a script that I dont know what is my matrix's dimention for any inputs. (again I pointed that If I want to write a general code for all inputs)
You don't need to know the exact size of the array you want to preallocate when you write the code, if you can determine or calculate the size of the array from information available before you enter the loop(s). I'm going to guess the limits on i and j are related to the size of COPT?
assert(ismatrix(COPT)) % rather than a 3-D or N-D (N > 2) array
sz = size(COPT);
% Preallocate A to be the same size as COPT
A = zeros(sz);
for whichrow = 1:sz(1)
for whichcol = 1:sz(2)
% Do stuff with A(whichrow, whichcol) and COPT(whichrow, whichcol)
end
end
I have no idea how large COPT will be when you run that code, but I do know A will start off the same size as COPT.
Daniel M
Daniel M 2019년 11월 14일
편집: Daniel M 2019년 11월 14일
Incremental growth of arrays and dynamic reallocation are tricky things in MATLAB (this is when you cannot know the size that an array will be before the loop occurs). You may be interested in the utility growdata on the File Exchange, where John D'Errico talks about and implements solutions to this problem:

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2019년 11월 13일

편집:

2019년 11월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by