简体   繁体   中英

How to preallocate cell array to impove speed in Matlab?

can anyone help me to understand how can i pre-allocate the output that I get in "statuses" (statuses is a Nx1 cell, where each cell is 1x1 struct) in order to increase the speed of the code? (tic-toc around 57sec)

thank you in advance!

tweetquery = 'hello';
s = search(c,tweetquery,'count',100);
statuses = s.Body.Data.statuses;
%
while isfield(s.Body.Data.search_metadata,'next_results') 
    nextresults = string(s.Body.Data.search_metadata.next_results);   
    max_id = extractBetween(nextresults,"max_id=","&");              
    cmax_id = char(max_id);                                                                                
    s = search(c,tweetquery,'count',100,'max_id',cmax_id);
    statuses = [statuses;s.Body.Data.statuses];
end

In general, you preallocate a cell array using the cell function:

statuses = cell(N,1);

However, in this case you don't know how many elements to expect. You can either allocate a very large cell array, larger than what you'll need, and then cut off the unused part at the end, or you must live with extending the array every loop iteration. If the computation that is performed within the loop is large enough, the cost of reallocating is not important.

To optimally extend the matrix every iteration, it is best to use the following syntax:

statuses{end+1} = new_element;

This assumes that new_element is a single element, not a cell array of elements that you need to append. The reason that this is better is that it allows MATLAB to optimize reallocation of the array. See this other Q&A for an experiment that demonstrates the difference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM