Warning on PERSISTENT statement
6 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!