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
Image Analyst 2020년 5월 26일
Looks like you forgot to attach 'SALS.m'.
Mina Pakzadmanesh
Mina Pakzadmanesh 2020년 5월 26일
Yes you are right. I attached it now.
Image Analyst
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
darova 2020년 5월 27일
Can you put outside the for loop this part of the code?
Mina Pakzadmanesh
Mina Pakzadmanesh 2020년 6월 3일
it takes around 3600 seconds.
when I use isfile instead of exist, shall I make any change in fscan ? because I got error.
Mina Pakzadmanesh
Mina Pakzadmanesh 2020년 6월 3일
I tried to put it out put it didnt work

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

답변 (1개)

Steven Lord
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에 대해 자세히 알아보기

태그

질문:

2020년 5월 26일

답변:

2020년 6월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by