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 Kommentare

Image Analyst
Image Analyst am 26 Mai 2020
Looks like you forgot to attach 'SALS.m'.
Mina Pakzadmanesh
Mina Pakzadmanesh am 26 Mai 2020
Yes you are right. I attached it now.
Image Analyst
Image Analyst am 27 Mai 2020
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 am 27 Mai 2020
Can you put outside the for loop this part of the code?
Mina Pakzadmanesh
Mina Pakzadmanesh am 3 Jun. 2020
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 am 3 Jun. 2020
I tried to put it out put it didnt work

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Steven Lord
Steven Lord am 3 Jun. 2020

0 Stimmen

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.

Kategorien

Tags

Gefragt:

am 26 Mai 2020

Beantwortet:

am 3 Jun. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by