global variables are fast? Functions make copies or not?

In the description, I read: "if several functions all declare a particular variable name as global, then they all share a single copy of that variable." This means that every funcion copy that variable and slow down the code? Or global variables can be used immidiatelly, and the copy is virtual?

5 Comments

There are no copies: as the docs say, there is only one variable stored in memory. Note that this also applies to normal input arguments: MATLAB uses what is called "copy on write", which means even a normal input argument is not copied in memory (until it is written). Source: https://blogs.mathworks.com/loren/2006/05/10/memory-management-for-functions-and-variables/
"global variables are fast?"
No, global variables are slow: "Finally, when a function call involves global variables, performance is even more inhibited. This is because to look for global variables, MATLAB has to expand its search space to the outside of the current workspace. Furthermore, the reason a function call involving global variables appears a lot slower than the others is that MATLAB Accelerator does not optimize such a function call"
Basically global variables are what beginners use instead of learning good code design:
etc, etc, etc.
Global variables are really slow, in addition to all the other reasons not to use them. When I took a piece of 3rd party code recently that was full of globals I speeded it up by a significant factor by doing nothing other then replacing the global variables with proper variables.
OK, but I cannot imagine a good coding practice in this case: https://www.mathworks.com/matlabcentral/answers/349189-is-it-possible-to-assign-a-table-to-a-function-for-speed-up I think, the only way is to use global variables, if the speed is enough for a certain problem. When runtime of a hard numeric calculation is my main problem, maybe the beauty of the code is not the most important thing, however I am looking for the best solution.
Well if you want runtime then don't use global variables. They are slow. Period.
Somewhere in the 2010 time frame, I wrote my own versions of some common sorting algorithms using recursion, of course passing fragments of vectors into and out of function calls. At that time, the performance was shockingly slow - for large amounts of data, quick sort, for example, was O(N sq). I was able to use global variables to improve that speed significantly. Then, some time later, I ran it again and the performance was O(N log N) as expected. After much inquiring, I discuvered that the Matlab function calling process had been optimized so that it did not pass a copy of an array into a function unless the function changed the data. That made passing arrays quicker than the global implementation.

Sign in to comment.

Answers (2)

Jan
Jan on 18 Jul 2017
Edited: Jan on 18 Jul 2017
There are many discussions about the performance of global variables in the net. Simply search in thuis forum or using an internet search engine.
Passing variables as input is faster than sharing them as globals from the viewpoint of the run-time. But if you take into account the time for maintaining and debugging the code, globals are an absolute DON'T. While code with some 1000s lines of code might work with globals, expaning the code or letting it run together with arbitrary other tools will lead to unsolvable problems. You cannot control, where the last changes has been made and cannot prevent other codes to use the same names for global variables as you do. Therefore it is recomended frequently, not to use globals at all.
By the way: Asking the forum is always a good idea. And you can simply check this by your own also. Write some code and implement the shared variable as input argument or as global. Then use timeit or tic/for k = 1:1000, yourCode; end, toc to measure the runtime.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 18 Jul 2017

Commented:

on 15 Jun 2020

Community Treasure Hunt

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

Start Hunting!