Find more time-efficient way to extract data from a .txt file

1 Ansicht (letzte 30 Tage)
Albert Garcia
Albert Garcia am 7 Mär. 2020
Kommentiert: dpb am 8 Mär. 2020
I have a code that takes over 4minutes to converge to a solution, and I need to get convergence about 30 times for synthesis purposes, resulting in over 2hrs of run time. I found that what is slowing my code down so much is the fopen function.
The files with the function name "intermediate_GW" and "intermediate_GW2" read a single data point with the format #####.# from a text file outputted from my gross weight solver (not listed above), and then, they output this value to the functions FF, DISK, and SW_TOTAL. "intermediate_GW2" stays the same per iteration of this WEIGHT_FUEL program while "intermediate_GW" changes per call. Is there a way for me to have the values from these two programs in a better format for this purpose? Below is a rough flowchart of how my programs are going to run. I can't have the TOTAL_WEIGHT program directly output to my other programs because this causes an infinite recursion, and I had to bypass that by using txt files.
If it could be done such that the below loop of intermediate_GW2, DISK, and SW_TOTAL can be called only once instead of 40000 times, this alone would drastically improve the runtime of my code.
  9 Kommentare
dpb
dpb am 8 Mär. 2020
"...with one text file changing and the other remaining constant."
That doesn't seem to be so...
...
i=i+1;
fileID = fopen('intermediate_GW.txt','w');
fprintf(fileID,'%.1f\n',GW);
fclose(fileID);
fileID = fopen('intermediate_GW2.txt','w');
fprintf(fileID,'%.1f\n',GW);
fclose(fileID);
end
writes the same value to both files every iteration.
Presuming the
GW = intermediate_GW2;
function is reading the file of that name, the new GW value in SW_TOTAL(y) is the same one just written.
In which case, just pass it (new GW) in the argument list and bypass the file.
When you get to the bottom and GW=sum(W), THEN you do have a difference again that you can test for convergence.
I can't see any reason for the file at all if you just pass the information as an additional argument to the functions that need it.
Albert Garcia
Albert Garcia am 8 Mär. 2020
In the programs above, it doesn't show the GW file changing values within the first text file, but it does change values when it is used by other functions.
That's how I first tried it, having the main file pass the information to the beneath arguments, but I couldn't figure out how to do that without infinite recursions, so I ended up with this inconvenient solution.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

dpb
dpb am 8 Mär. 2020
While I'm convinced you really don't need the files at all and probably with only a minor amount of rearranging if it works as is, one really expedient workaround would be to not open/close the files every time...
function [GW,W] = WEIGHT_SIZING(H,IT,x,y)
...
i = 1; %Used for iterations
fID1 = fopen('intermediate_GW.txt','w');
fID2 = fopen('intermediate_GW2.txt','w');
...
while epsilon > 1
...
i=i+1;
frewind(fID1)
fprintf(fID1,'%.1f\n',GW);
frewind(fID1)
frewind(fID2)
fprintf(fID2,'%.1f\n',GW);
frewind(fID2)
end
fID1=fclose(fID1); % release file handles only when completed iteration loop
fID2=fclose(fID2);
end
You then either add fID1, fID2 to the argument list to the functions that need them, or, (ugly, but expedient) make them GLOBAL variables.
  5 Kommentare
Albert Garcia
Albert Garcia am 8 Mär. 2020
Well, I will be using this program quite a bit for the next month, but I don't think I should invest more time in making this code better since it's good enough now. I do need to learn how to properly structure my code when I write it though; my terrible structure skills are definitely what have made me waste so much time fixing this code when I should've written it write the first time around.
dpb
dpb am 8 Mär. 2020
Your call, of course. If it's fast enough this way, then perhaps there's no real justification for spending the time now to refactor it.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Parallel and Cloud finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by