1

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
Demi
  • 15
  • 5

1 Answers1

1

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.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120