Efficienty in accessing memory

3 次查看(过去 30 天)
I am writing an interative algorithm that essentially consits of a big for loop with some sub-loops. At every iteration of the outer loop I have to save some variables in a cell array called MC (I have pre-allocated the memory for MC). The variables saved in one iteration are not used in subsequent iterations. Is it more efficient to create a "temporary variable" to be used inside every iteration, saved in the MC cell-array and then overwritten the nex iteration or to directly create the variable in the cell-array and use it for the operations inside the iteration?
For example, is it more efficient a thing like:
for t=2:1e5
N = floor(log(min(u * c)) / log((c / (kappa * (1 + c)))));
MC{t}.N = N;
% vector of nj values
n_vec = sum(repmat(d, 1, N) == repmat(1:N, T, 1))';
n_vec2 = sum(repmat(d, 1, N) > repmat(1:N, T, 1))';
...
end
or something like:
for t = 2 : 1e5
MC{t}.N = floor(log(min(u * c)) / log((c / (kappa * (1 + c)))));
% vector of nj values
n_vec = sum(repmat(d, 1, MC{t}.N) == repmat(1:MC{t}.N, T, 1))';
n_vec2 = sum(repmat(d, 1, MC{t}.N) > repmat(1:MC{t}.N, T, 1))';
....
end
Keep in mind that this is only an example: there are 12 variables that behaves like N in the example and some of them are quite big tridimensional arrays.
P.S. I am running the algorithm using version 2015a on a Server with a 64bit OS, a RAM of 128GB and a 3.5GHz CPU
Thanks
  2 个评论
Cedric
Cedric 2015-9-15
编辑:Cedric 2015-9-15
It is difficult to say a priori, because it is difficult to anticipate what JIT will do exactly. You should build a small benchmark and profile all relevant approaches (using both the profiler [type profile viewer in the command window and run your script there] and tic/toc). You may realize that REPMAT is the/a bottleneck and try to implement some alternate approach based on matrix multiplication with a precomputed matrix and/or some call to BSXFUN.
James Tursa
James Tursa 2015-9-15
编辑:James Tursa 2015-9-15
How are you pre-allocating MC? Just the MC, or all of the MC{etc}.N? Can you use a simple double vector for these values instead of a field of a struct of a cell array? Seems like you are re-doing some calculations when forming n_vec and n_vec2.

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2015-9-15
If you were using the Parallel Computing Toolbox, you would need to compute everything into a temporary variable and then write it in to the output indexed by the parfor loop index.
The JIT compiler is able to detect variables that are completely overwritten each time, and handle them more efficiently -- though it would not hurt to clear the variable at the end of the loop to hint even more strongly.
  2 个评论
Nicola Donelli
Nicola Donelli 2015-9-15
Sorry, I forgot to specify that I am not using the Parallel Toolbox since the algorithm cannot be parallelized.
Walter Roberson
Walter Roberson 2015-9-15
None the less, I would expect better optimization by using loop-local variables to build the value to be saved as an aggregate.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by