Will splitting a for loop up make code run quicker?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have some code where I read data from a file then perform some pretty heavy anaylsis on it. This is repeated for 650 files and the whole process is taking around 3 hours to run. The general code is:
for i = 1:650
file_name = sprintf('file%f', i);
load(file_name);
.......... %rest of code
end
As the same actions are performed independently on each file, I was wondering if there's anyway to split this for loop up so that the code can be ran on several files siultaneously instead of having to wait for the previous loop to finish. Would this even make my code run quicker? I've briefly heard about 'parallel computing' before, but I'm not too familar with the concept or how to implement it.
Thanks.
9 commentaires
Stephen23
le 8 Jan 2022
"Would this even make my code run quicker?"
No. Yes. Maybe. Who knows?
Any potential benefit of using parallel computation depends on many factors that we do not know about your code.
However, it is best to avoid trying to micro-manage MATLAB's JIT optimization: when beginners try to do something very cunning to speed things up it just gets in the way of JIT engine (which is written to speed up clearly-written, well-organized code).
Jumping onto the parallel-computation bandwagon is certainly not a alternative to writing better code, for example:
- replacing directly LOADing into the workspace with LOADing into an output variable.
- not saving your code in the same location as your data files, using absolute/relative filenames instead.
- avoiding variable name i.
- no doubt many other improvements in the code that you did not show us.
"I've briefly heard about 'parallel computing' before, but I'm not too familar with the concept or how to implement it"
Learning good MATLAB practices would be a much better use of your time, until you can find a bottle-neck that really can only be solved using parallel computation.
Réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!