Warning on PERSISTENT statement
6 views (last 30 days)
Show older comments
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 Comments
Answers (0)
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!