Warning on PERSISTENT statement

6 Ansichten (letzte 30 Tage)
Bruno Luong
Bruno Luong am 30 Jul. 2019
Kommentiert: Rik am 11 Apr. 2020
I know that PERSISTENT statement is slow, and try to overcome by passing an logical argument to by-pass when I know the function doesn't need using the persistent variable. This illustrate by my FOO function
function a = foo(astatic)
if astatic
% mlint: "PERSISTENT could be very inefficient unless it is a top-level
% statement in its function"
persistent A
a = A;
else
a = 3;
end
end % of foo
But then MATLAB gives the warning I quote abobe.
To clear thing out, I made a comparison of runing time with BAR function
function a = bar(astatic)
% No warning
persistent A
if astatic
a = A;
else
a = 3;
end
end % of bar
And then when I test (R2019a, windows) I get those results
tic;
for k=1:1000000
foo(0);
end
toc % Elapsed time is 0.076238 seconds.
%%
tic;
for k=1:1000000
foo(1);
end
toc % Elapsed time is 0.784971 seconds.
%%
tic;
for k=1:1000000
bar(0);
end
toc % Elapsed time is 0.764904 seconds.
%%
tic;
for k=1:1000000
bar(1);
end
toc % Elapsed time is 0.790119 seconds.
As you can see, the results do not backup MLINT message.
Can I just ignore it or is there anything else I miss?
  4 Kommentare
Bruno Luong
Bruno Luong am 31 Jul. 2019
Bearbeitet: Bruno Luong am 31 Jul. 2019
I don't think PERSISTENT would be implement as GLOBAL.
Runing time, persistent vs global: I can't see why the MATLAB somewhere else where the PERSISTENT variables exists. It is right there just like any local variable, however the internal mxArrary structure should be stored somesort of heap/static memory zone and it must not be cleared as local variables when the function exits.So PERSISTENT does not require MATLAB to b search in some sort of data base (using hash?), and it should be faster than GLOBAL. And it is (about 7/8 fold) as showed here:
function a = glob(astatic)
if astatic
global A
a = A;
else
a = 3;
end
end % of glob
function a = foo(astatic)
if astatic
% mlint: PERSISTENT could be very inefficient unless it is a top-level
% statement in its function
persistent A
a = A;
else
a = 3;
end
end % of foo
%%
tic;
for k=1:1000000
glob(0);
end
toc % Elapsed time is 0.099111 seconds.
%%
tic;
for k=1:1000000
glob(1);
end
toc % Elapsed time is 5.843688 seconds.
%%
tic;
for k=1:1000000
foo(0);
end
toc % Elapsed time is 0.076238 seconds.
%%
tic;
for k=1:1000000
foo(1);
end
toc % Elapsed time is 0.784971 seconds.
%%
tic;
for k=1:1000000
bar(0);
end
toc % Elapsed time is 0.764904 seconds.
%%
tic;
for k=1:1000000
bar(1);
end
toc % Elapsed time is 0.790119 seconds.
My my question is indeed similar to other post about the correctness of MLINK message and it is obviously a wrong warning for both global and persistent usage.
Rik
Rik am 11 Apr. 2020
So actually the warning should be "this usage can lead to unexpected rersults"?

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by