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!