MATLAB Answers

How to improve Simulink.Parameter objection creation time?

1 view (last 30 days)
Jed Frey
Jed Frey on 17 Apr 2017
Edited: Jed Frey on 17 Apr 2017
For our embedded coder application we are using a Simulink.Parameter for most of our tunables.
Profiling the model init we've found that creating the Simulink.Parameters is becoming a considerable part of our workflow's "sit around and wait time". We've already optimized out anything else we could.
I've tried taking a stab at a few ideas of improving speed but nothing has worked so far. This is the function I created to profile what is eating the most time along with results:
function [A, B, C] = benchmark_ParameterCaching
% Benchmark SimulinkParameter Creation
% Number of elements to create.
N = 1e4;
% Preallocate cell arrays.
tic;
A = cell(1,N);
B = cell(1,N);
C = cell(1,N);
fprintf('Cell Creation: %.2f\n',toc);
% Test the speed of no caching.
tic;
for i=1:N
A{i} = Simulink.Parameter;
A{i}.Value = i;
end
fprintf('Simulink.Parameter: %.2f\n',toc);
tic;
for i=1:N
B{i} = SimulinkParameterCache;
B{i}.Value = i;
end
fprintf('SimulinkParameterCache: %.2f\n',toc);
tic;
for i=1:N
C{i} = SimulinkParameterCopyCache;
C{i}.Value = i;
end
fprintf('SimulinkParameterCopyCache: %.2f\n',toc);
% Test the speed of saving 3*N Simulink.Parameterss
tic;
save('SimulinkParameterCache.mat','A','B','C')
fprintf('save: %.2f\n',toc);
% Clear the variables
tic;
clear A B C
fprintf('clear: %.2f\n',toc);
% Test the speed of loading 3*N Simulink.Parameterss
tic;
load('SimulinkParameterCache.mat','A','B','C')
fprintf('load: %.2f\n',toc);
function parameter = SimulinkParameterCopyCache
% Create a cache to persist across
persistent cache
% If the persistent variable does not exist the first time you issue
% the persistent statement, it will be initialized to the empty matrix.
if isempty(cache)
cache = Simulink.Parameter;
end
% Copy the existing empty instance and return it.
parameter = cache.copy;
function parameter = SimulinkParameterCache
% Create a cache to persist across
persistent cache
% If the persistent variable does not exist the first time you issue
% the persistent statement, it will be initialized to the empty matrix.
if isempty(cache)
cache = Simulink.Parameter;
end
% Copy the existing empty instance and return it.
parameter = cache;
Results:
>> [A, B, C] = benchmark_ParameterCaching;
Cell Creation: 0.00
Simulink.Parameter: 7.70
SimulinkParameterCache: 0.22
SimulinkParameterCopyCache: 7.89
save: 4.98
clear: 0.24
load: 25.49
Profiling output:
All of the top time consumers are .p files that prevent any further inspection.
The non-copy cache is a nonstarter because it just references the same Simulink.Parameter so changing B{1} affects B{2:end}.
I was hoping to gain some speed up by saving parameters off to a .mat file but reloading it ended up taking ~50% more time than just creating them from scratch to begin with:
25.49/(6.78+.22+7.89)
ans =
1.7119
Is there any way to speed up Simulink.Parameter creation? 50 engineers waiting 10 seconds to create Simulink.Parameters when opening a model 5 times per day, 5 days a week, 50 weeks a year is 175 business hours.

Answers (0)

Community Treasure Hunt

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

Start Hunting!