what is the efficient way to search for value in a large struct
6 views (last 30 days)
i want to serch for value in spacific field ("Config") of very larg struct array (1e6 rows).
i do this serch all the time Therefore, I need the fastest way...
this is the way i try:
tree(1e6) = struct("Index",,"parent", ,"Config",,"Movment",)
just for the exemple i feel the field Config with value...
[tree.Config] = deal(1);
becose of the size of this struc the serch is very slow.
i try diffrent metode like:
value2find = 15;
config = [tree.Config];
loc = ismember(config,value2find);
in this metod extruct all the value from the struct take long time:
this way is the worst:
Loc = arrayfun(@(x) ismember(value2find,x.Config),tree);
timeit(@() arrayfun(@(x) ismember(value2find,x.Config),tree))
I thought of a few ideas to deal with the problem, first I will detail the algorithm(RRT*) in a slightly abstract way:
I have a loop that produces random values according to a certain regularity, each value is checked to see if it exists in the struct array. If the value exists, an update operation is performed for the same row in the struct array, if the value does not exist then it is added to the struct array in the first available space.
- Should I save the field I am looking for as a numeric array separately from the structure? So the search will be more efficient but will it require me to update both the array and the structure each time?
- Maybe instead of a structure, I should use a cell array? Will it be significantly faster?
- I will emphasize that the struct array is empty of values at the beginning and fills with time, it may be worthwhile to limit the search to only the part that has values, of course as time goes by there will be more values and therefore the search time will increase accordingly.
I would love for suggestions and better ways than I was able to do
More Answers (1)
Johan on 2 Mar 2022
I'm not sure that fits what you want but you could try making large arrays in a small structure instead of the opposite:
tree(1) = struct("Index",,"parent", ,"Config",,"Movment",);
tree.Config = rand(1e6,1);
tree2(1e6) = struct("Index",,"parent", ,"Config",,"Movment",);
value2find = 15;
for i = 1:100
loc = ismember(tree.Config,value2find);
timer(i,1) = toc;
loc = sum([tree2.Config]==value2find);
timer(i,2) = toc;