Extracting bus elements and their signals from a data dictionary has very slow performance.
Show older comments
I have inhertited a script that I recently updated to search a data dictionary instead of the base workspace to extract buses and signals. It takes ~4 hours to process ~140 buses /~7000 signals in the data dictionary. When these signals were in the base workspace the processing time was 1.6 seconds.
The profile viewer shows that this line consumes 99% of the script if (exist('ddatasectobj','var')) && (~isempty(find(find(ddatasectobj,'-value','-class','Simulink.Bus'),'Name',buselements{nelements,2})))
Is it a feature of using the data dictionary or is there a better method and I am just missing it?
%iterate through each element to find any nested buses, then
%recursively get all those bus elements... and so on...
for nelements=1:size(buselements,1)
% Remove Bus: prefix from datatype string
buselements{nelements,2} = regexprep(buselements{nelements,2},'^[Bb]us:(\W*|)','');
if (exist('ddatasectobj','var')) && (~isempty(find(find(ddatasectobj,'-value','-class','Simulink.Bus'),'Name',buselements{nelements,2})))
buselements{nelements,5}='bus';
%if haven't already defined, recursively call this function to get all nested buses
if ~sum(strcmp(buselements(1:nelements-1,2), buselements{nelements,2}))
busobjects=[busobjects;getbusdef(slddname,buselements{nelements,2})];
end
Answers (1)
Ashutosh Thakur
on 26 Jun 2024
Hi Dawn,
I understand that you are trying to search through a data dictionary but are facing slow performance while executing these queries. The increase in processing time between accessing the base workspace and a data dictionary in Simulink is likely due to the overhead associated with these queries. As you mentioned, a particular query consumes 99% of the overall time during the execution of your script.
The following steps can be followed to improve performance:
Cache Repeated Searches:
- You can try caching the repeated searches in the form of a MATLAB variable or a MAT file. This will prevent querying the data dictionary again and use in-memory results for better performance. Refer to the following documentation link to improve the performance of MATLAB code:https://www.mathworks.com/help/matlab/matlab_prog/techniques-for-improving-performance.html.
Bulk Data Retrieval:
- If you have a bulk of data, try executing the query to get dictionary data without using loops, and then store this data in a MATLAB variable. For example, the following sample code will retrieve all the Simulink.Bus objects from the data dictionary section object passed to it:
% ddSectObj is the Simulink.data.dictionary.Section object
find(ddSectObj, '-value', '-class', 'Simulink.Bus').
- Store the results of the above-mentioned code in a MATLAB variable and loop through only the bus objects.
- Also refer to the following documentation link to use find and getSection functions: https://www.mathworks.com/help/matlab/ref/find.html. https://www.mathworks.com/help/simulink/slref/simulink.data.dictionary.getsection.html
Use Parallel Computing:
- Consider leveraging MATLAB's Parallel Computing Toolbox to parallelize some of the operations to reduce execution time. Refer to the following documentation link for more information: https://www.mathworks.com/help/parallel-computing/quick-start-parallel-computing-in-matlab.html.
I hope this helps you in improving the performance.
Categories
Find more on String in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!