Does using Functions make your code run faster?
조회 수: 19 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
답변 (1개)
Bruno Luong
2020년 12월 8일
편집: Bruno Luong
2020년 12월 8일
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Performance and Memory에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!