using global variables to avoid passing by value
이전 댓글 표시
It is often heard that using global variables is a bad practice.
However, personally, I often declare some variable as global to avoid passing its value to a function call.
My concern is, the function is to be called many many times, and if each time the variable which is a big chuck of data is to be passed by value to the function, it would take a lot of time for copying the data.
Therefore, is using global variable really a bad idea in this circumstance?
BTW, the global variable is not changed by the function in my case.
채택된 답변
추가 답변 (1개)
alelap
2024년 4월 6일
2 개 추천
I agree with Jonh D'Errico's answer.
MATLAB uses a so called "copy-on-write" mechanism that is exactly designed to tackle such a circumstance:
카테고리
도움말 센터 및 File Exchange에서 Performance and Memory에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!