Why DatasetRef 'get' method is faster with index rather than name?
Show older comments
I would like to load some elements from a big dataset (output of a Simulink simulation).
I decided to use the Simulink.SimulationData.DatasetRef() fuction to avoid loading the entire dataset in my workspace. For example:
ref_data = Simulink.SimulationData.DatasetRef(saving_path, "logout");
Then, I tried to use the get() method of the DatasetRef to load some elements. I noticed that if I pass the element name, the method is slow, whereas if I pass the element index, the method is much faster.
Here there is an example:
clear
saving_path = 'dataset.mat';
el_name = 'el_name';
ref_data = Simulink.SimulationData.DatasetRef(saving_path, "logout");
tic
a = ref_data.get(el_name).Values;
disp('Time with name:')
toc
tic
index = find(strcmp(ref_data.getElementNames, el_name));
b = ref_data.get(index).Values;
disp('Time with index:')
toc
if isequal(a,b)
disp('a and b are equal')
end
The result is:
Time with name:
Elapsed time is 4.327172 seconds.
Time with index:
Elapsed time is 0.035908 seconds.
a and b are equal
(tested in Matlab R2024b abd Matlab R2022b)
Why does the call with the element name take much more time?
The solution with the index is simple and effective, but less readable.
3 Comments
Walter Roberson
on 3 Jun 2025
Hypothetically, it is possible that it is caching results after the first try. You should experiment with running the tests in the other order and seeing whether the times are consistent or instead the first attempt is longer.
Marco
on 4 Jun 2025
Walter Roberson
on 4 Jun 2025
Interesting. I would expect minor differences, but no-where near the difference that you see.
Accepted Answer
More Answers (0)
Categories
Find more on Outputs 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!