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!