Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does using global variables impact performance in MATLAB?

As I understand, MATLAB cannot use pass by reference when sending arguments to other functions. I am doing audio processing, and I frequently have to pass waveforms as arguments into functions, and because MATLAB uses pass by value for these arguments, it really eats up a lot of RAM when I do this.

I was considering using global variables as a method to pass my waveforms into functions, but everywhere I read there seems to be a general opinion that this is a bad idea, for organization of code, and potentially performance issues... but I haven't really read any detailed answers on how this might impact performance...

My question: What are the negative impacts of using global variables (with sizes > 100MB) to pass arguments to other functions in MATLAB, both in terms of 1) performance and 2) general code organization and good practice.

EDIT: From @Justin's answer below, it turns out MATLAB does on occasion use pass by reference when you do not modify the argument within the function! From this, I have a second related question about global variable performance:

Will using global variables be any slower than using pass by reference arguments to functions?

like image 984
RTbecard Avatar asked Dec 24 '15 10:12

RTbecard


People also ask

Do global variables slow down program?

Overusing local and global variables, such as using them to avoid long wires across the block diagram or using them instead of data flow, slows performance.

Is it better to use local or global variables?

It all depends on the scope of the variable. If you feel that a certain variable will take multiple values by passing through various functions then use local variables and pass them in function calls. If you feel that a certain variable you need to use will have constant value, then declare it as a global variable.

Are global variables faster?

The first is that allocating global variables may be faster, since they are only allocated once, at the time the program is first spawned, whereas local variables must be allocated every time a function is called.

Is it good to use global variables?

Throughout the programming world, there is the notion that using global variables is bad practice. Global variables are generally avoided because they threaten “encapsulation”, or in other words, the ability of the script to control the ability to access information inside an object.


3 Answers

MATLAB does use pass by reference, but also uses copy-on-write. That is to say, your variable will be passed by reference into the function (and so won't double up on RAM), but if you change the variable within the the function, then MATLAB will create a copy and change the copy (leaving the original unaffected).

This fact doesn't seem to be too well known, but there's a good post on Loren's blog discussing it.

Bottom line: it sounds like you don't need to use global variables at all (which are a bad idea as @Adriaan says).

like image 78
Justin Avatar answered Oct 13 '22 20:10

Justin


While relying on copy on write as Justin suggested is typically the best choice, you can easily implement pass by reference. With Matlab oop being nearly as fast as traditional functions in Matlab 2015b or newer, using handle is a reasonable option.

like image 3
2 revs, 2 users 67% Avatar answered Oct 13 '22 21:10

2 revs, 2 users 67%


I encountered an interesting use case of a global variable yesterday. I tried to parallellise a piece of code (1200 lines, multiple functions inside the main function, not written by me), using parfor.

Some weird errors came out and it turned out that this piece of code wrote to a log file, but used multiple functions to write to the log file. Rather than opening and closing the relevant log file every time a function wanted to write to it, which is very slow, the file ID was made global, so that all write-functions could access it.

For the serial case this made perfect sense, but when trying to parallellise this, using global apparently breaks the scope of a worker instance as well. So suddenly we had 4 workers all trying to write into the same log file, which resulted in some weird errors.

So all in all, I maintain my position that using global variables is generally a bad idea, although I can see its use in specific cases, provided you know what you're doing.

like image 3
Adriaan Avatar answered Oct 13 '22 21:10

Adriaan