Will splitting a for loop up make code run quicker?

2 visualizaciones (últimos 30 días)
S
S el 6 de En. de 2022
Comentada: S el 8 de En. de 2022
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 comentarios
Stephen23
Stephen23 el 8 de En. de 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:
  1. replacing directly LOADing into the workspace with LOADing into an output variable.
  2. not saving your code in the same location as your data files, using absolute/relative filenames instead.
  3. avoiding variable name i.
  4. 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.
S
S el 8 de En. de 2022
@Stephen Thanks for the advice, I'll take it on board. Unfortunately, I can't share my code as it's for a college project.
I ended up downloading the Parallel Computing Toolbox, and simply changing my 'for' loop into a 'parfor' loop resulted in my program becoming 3 times quicker, so thanks for the recommendation @per isakson

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by