How can I make my code run faster?
Ältere Kommentare anzeigen
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
am 26 Mai 2020
Looks like you forgot to attach 'SALS.m'.
Mina Pakzadmanesh
am 26 Mai 2020
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
am 27 Mai 2020
Can you put outside the for loop this part of the code?

Mina Pakzadmanesh
am 3 Jun. 2020
Mina Pakzadmanesh
am 3 Jun. 2020
Antworten (1)
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
Mehr zu File Operations finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!