Warning on PERSISTENT statement
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Rik
el 11 de Abr. de 2020
So actually the warning should be "this usage can lead to unexpected rersults"?
Respuestas (0)
Ver también
Categorías
Más información sobre Startup and Shutdown en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!