How can I make my code run faster?
이전 댓글 표시
How can I make this code run faster?
clear all
clc
data = load('SALS.m');
alpha=data(:,1);
mean=data(:,2);
NETC='NET_ALPHA2/%i/net_%i'; % Network Library Path
NETS='Model_784EL_new/net_%i'; % Destination Path
fid=fopen('missing.txt','w');
for i=1:length(alpha)
NET1= sprintf(NETC,alpha(i),mean(i));
if exist(NET1, 'file') == 0
fprintf(fid,'%d\t%d\n',alpha(i),mean(i));
mean(i)=mean(i)+1;
NET1= sprintf(NETC,alpha(i),mean(i));
if exist(NET1, 'file') == 0
if alpha(i)==0
alpha(i)=1;
end
fprintf(fid,'%d\t%d\n',alpha(i),mean(i));
mean(i)=mean(i)-2;
NET1= sprintf(NETC,alpha(i),mean(i));
if exist(NET1, 'file') == 0
fprintf(fid,'%d\t%d\n',alpha(i),mean(i));
mean(i)=mean(i)-1;
NET1= sprintf(NETC,alpha(i),mean(i));
if exist(NET1, 'file') == 0
fprintf(fid,'%d\t%d\n',alpha(i),mean(i));
mean(i)=mean(i)+4;
NET1= sprintf(NETC,alpha(i),mean(i));
end
end
end
% continue
% else
end
NET2= sprintf(NETS,i);
fileID = fopen(NET1,'r');
fileID2 = fopen(NET2,'w');
B= fscanf(fileID,'%d %d %d\t',[1, 3]);
fprintf(fileID2,'%d %d %d\n',B);
formatSpec = '%d %d %d %f %f %f %f %f %f\n';
A = fscanf(fileID,formatSpec,[9,inf]);
A = A';
[m,n] =size(A);
for row = 1:m
fprintf(fileID2,formatSpec,A (row,:));
end
fclose(fileID);
fclose(fileID2);
% end
end
댓글 수: 6
Image Analyst
2020년 5월 26일
Looks like you forgot to attach 'SALS.m'.
Mina Pakzadmanesh
2020년 5월 26일
Image Analyst
2020년 5월 27일
I'd open as 'wt' and 'rt' since they are known to be text files, not binary files. But that probably won't make it run faster. Since you're doing serial input/output I don't know of anyway to make it faster. It looks like it should talke just milliseconds. How long does it take? Also you can use the new isfile() and isfolder() instead of exist(). Also to be more robust you should check the file handles and take appropriate action if it fails and they equal -1.
darova
2020년 5월 27일
Can you put outside the for loop this part of the code?

Mina Pakzadmanesh
2020년 6월 3일
Mina Pakzadmanesh
2020년 6월 3일
답변 (1개)
Steven Lord
2020년 6월 3일
0 개 추천
My suspicion is that if you were to profile this code the exist calls would consume the most time. If I understand what you're trying to do correctly, I think calling dir once to obtain the list of files and then checking the list in memory may be more efficient.
But I'd start off profiling the code to determine if my suspicion is correct about where the bottleneck are in the code.
카테고리
도움말 센터 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!