Why has matlab become extremely inefficient with global variables?

16 Ansichten (letzte 30 Tage)
Neil Marcus
Neil Marcus am 31 Jan. 2022
Kommentiert: Steven Lord am 28 Feb. 2022
I have a program which I have been running for years on various versions of Matlab. It uses a file of global variables, which I pass between functions. Running profiler:
In Matlab 2012b, the global file took 6 s out of a total 54 s running time
In Matlab 2017a, the global file took 67 s; all the rest was similar to 2012b
In Matlab 2020b, the global file took 320 s; all the rest was similar to 2012b
I realize that using global variable makes me a bad person, but I am not a programmer, don't use classes and just want matlab to help me with my calculations. I don't see why matlab performance should degrade by a factor of 50 in new versions?
  3 Kommentare
Image Analyst
Image Analyst am 3 Feb. 2022
Like James says (expand the comments), include a .mat file with your global variables. Are you talking about how long it takes to declare global variables in program1.m that have already been read into program1.m? Like
program1.m
g1 = rand(10000); % Somehow create the globa variables and then make them global.
g2 = rand(3000);
tic
global g1
global g2
toc % See how long it takes to put those variables into the "global" workspace.
program2.m
tic
global g1
global g2
toc % Shows 320 seconds.
Or are you perhaps reading the variables in from a mat file or somehow otherwise creating them in program1.m and that's how long it takes to read them in. Exactly how big (how many megabytes) are these variables?
Neil Marcus
Neil Marcus am 13 Feb. 2022
I did not read from a mat file, and the variables were already defined. A symbolic representation of my issue is: I had a file globals.m defining globals; about 250 scalars, so no big memory issue.
globals.m
global a1
global a2
...
Then I had a program prog2 with various subfunctions using the variables in a loop
prog2.m
globals
a1 = 23;
a2 = 432;
...
for jj = 1:1000
subprog
end
function subprog
globals
a1 = a1+2;

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Jan
Jan am 13 Feb. 2022
Bearbeitet: Jan am 13 Feb. 2022
You do not use global variables, but a script. Although you declare the varaibels as globals inside the script, there is no need to do so.
Method 1:
% globals.m
function globals
global a1
global a2
a1 = rand;
a2 = rand;
end
% prog2.m
function prog2
global a1
global a2
% No call of function globals here!
b = a1 + a2;
subprog();
end
function subprog
global a1
a1 = a1+2;
end
I recommend to avoid global variables and scripts. Try this:
Method 2:
% globals.m
function G = globals
G.a1 = rand;
G.a2 = rand;
end
% prog2.m
function prog2
G = globals();
a1 = G.a1;
a2 = G.a2;
b = a1 + a2;
G = subprog(G);
end
function G = subprog(G)
G.a1 = G.a1+2;
end
I assume the slow down is not cause by using global variable, but scripts. Matlab processes functions for efficiently.
Defining 250 scalars sounds a little bit strange. Did you you the profiler to find the bottleneck of the code in the modern Matlab versions?
  3 Kommentare
Hiro Yoshino
Hiro Yoshino am 20 Feb. 2022
You've been using MALTAB for a while, then why don't you try to use "Compatibility report"?
You may find better alternate ways or functions that reproduce what you are seeing with the curret code.
James Lebak
James Lebak am 23 Feb. 2022
We took a look at the sample code from your comment, and we're unable to reproduce the slowdown that you're seeing. If this is still a concern for you or blocking work that you need to do, please open a support ticket so that we can get more details.

Melden Sie sich an, um zu kommentieren.


Neil Marcus
Neil Marcus am 27 Feb. 2022
I have made and tested a test example below. In Matlab R2010b (32 bit), tic, for jj = 1:10000, a; end; toc takes 1.87 secs. in matlab 2020b (64 bit) it takes 56 secs
File globals.m
global a1
...
global a25
File b.m
globals
a1 = rand;
...
File a.m
globals
a1 = rand;
...
a24 = rand;
b
  2 Kommentare
Jan
Jan am 28 Feb. 2022
The rough paraphrazation of your code is not useful. What happens in the "..."lines? Why do you use a set of scripts to define global variables? Global variables help to share data between functions, while scripts share the variables already with their callers. This means, that the code is a complicated method to impede the efficient access of variables.
Plese post the complete code. Then suggesting an efficient modification does not require to guess your code.
Steven Lord
Steven Lord am 28 Feb. 2022
Or if posting the complete code is not an option for whatever reason, do as @James Lebak suggested and "please open a support ticket so that we can get more details." You can open a support ticket from this page.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Variables finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by