roishik
roishik

Reputation: 515

When is it effective to use global variables in MATLAB?

I'm writing a MATLAB code that one of its inputs is an 1 MP RGB image. This image is not been changed along the whole process.

In my code, the image is been sent from the main function into its' sub-functions etc. The process is really slow, and I'm trying to make the code more efficient. Is it more effective to define the image as a global variable and call it in any function without sending it back and forth?

Thanks!

Upvotes: 0

Views: 158

Answers (3)

Tommaso Belluzzo
Tommaso Belluzzo

Reputation: 23675

This represents a situation in which the global modifier can be used on the image variable as follows:

global img;
img = imread('...');

x = process_image();

function x = process_image()
    global img; % this brings the img variable with its current value

    a = process_image_a();
    b = process_image_b();
    x = ...; % do something with a, b and img
end

function a = process_image_a()
    global img; % this brings the img variable with its current value
    a = ...; % do something with img
end

function b = process_image_b()
    global img; % this brings the img variable with its current value
    b = ...; % do something with img
end

The drawback of this approach is that you have to redeclare the variable using the global keyword within every scope (functions, workspaces, etc...) in which you want it to be shared across. While this can reduce the complexity of your functions and improve the readability of your code (is it really necessary?), by no means it will improve the performance of your code.

While the in-memory location of parameters is known, the location of a global variable must be "searched" at runtime every time the global keyword is encountered. Hence, global variables will always be slower.

If you compare the performance of the above code snipped with the performance of the code below (provided you perform the same computations in both):

img = imread('...');
x = process_image(img);

function x = process_image(img)
    a = process_image_a(img);
    b = process_image_b(img);
    x = ...; % do something with a, b and img
end

function a = process_image_a(img)
    a = ...; % do something with img
end

function b = process_image_b(img)
    b = ...; % do something with img
end

you will see that the latter will always performs slightly better than the former.

Overall, in any case, this is a minor performance issue. You should debug and profile your functions in order to detect the real bottlenecks and improve their execution time before being concerned about this.

Upvotes: 3

Alex Finch
Alex Finch

Reputation: 34

You could call global image when declaring it and when you need to use it you just call global image again. For example

%Start of code
global image
image = (however you got your image)


Function one()
global image;
%use image for whatever it needs to be used for
end

Upvotes: 0

Sairus
Sairus

Reputation: 394

Do you change that image in many functions? Matlab has copy-on-write mechanism that prevents copying input arguments unless they are changed inside the function.

I have solved such problem writing handle class as 'containers' to my shared data. Just create new handle class file and apply every change in its data fields, e.g.:

classdef BufStorage < handle
    properties
        Data
    end
end

Upvotes: 1

Related Questions