I think an inline cell structure would be suitable for storing growing structures. I made a comparison between:
- Dynamic cell size, cell size changes every cycle
- Pre-selected cell
- Java LinkedList
the code:
clear; scale = 1000; % dynamic size cell tic; dynamic_cell = cell(0); for ii = 1:scale dynamic_cell{end + 1} = magic(20); end toc % preallocated cell tic; fixed_cell = cell(1, scale); for ii = 1:scale fixed_cell{ii} = magic(20); end toc % java linked list tic; linked_list = java.util.LinkedList; for ii = 1:scale linked_list.add(magic(20)); end toc;
Results:
Elapsed time is 0.102684 seconds. % dynamic Elapsed time is 0.091507 seconds. % pre-allocated Elapsed time is 0.189757 seconds. % Java LinkedList
I change scale and magic(20) and find that dynamic and pre-selected versions are very close in speed. Perhaps the cell only stores pointer structures and is effective at resizing. The Java path is slower. And I find it sometimes unstable (it breaks my MATLAB when the scale is very large).
Edward shi
source share