Does using Functions make your code run faster?

19 visualizaciones (últimos 30 días)
Christoper Angelo Malayan
Christoper Angelo Malayan el 8 de Dic. de 2020
Editada: Bruno Luong el 8 de Dic. de 2020
Hi all. I'm new in coding in general and in Matlab. I've made some sort of modified capacitated k-means clusterring that clusters more than 30,000 points of data into groups with similar features.
I've done this using only scripts and NOT a single function. Currently, 100 iterations takes about 1hr to complete. I intend to do more than 100 iterations and I intend to add more data points. I'm trying to find ways on how to make this run faster. This said, does converting most of the instructions in the code into functions make it run faster?
From what I've understand so far, scripts save variables in the current workspace while functions does not. But I fail to understand how can this ever matter in terms of the speed of the code implementation provided that in both scenario, data is stored and retrieved from memory. I'm not a data scientist of any kind. Could someone explain how this works?
P.S. I have a Ryzen 5 3550h processor and a 5.9gb usable RAM if computer specs matters.
Any help is greatly appreciated. Thank you.

Respuestas (1)

Bruno Luong
Bruno Luong el 8 de Dic. de 2020
Editada: Bruno Luong el 8 de Dic. de 2020
MATLAB makes optimization when the code is in function, it does not in script (why it's also mysterious to me, there might be the reason but folks in MATLAB never bother to explain to the end users).
Whereas it matters depending on your code, how much CPU calculation from your script itself or functions that are called by your script.

Categorías

Más información sobre Performance and Memory 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