Efficienty in accessing memory
1 回表示 (過去 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
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
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
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 件のコメント
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 Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!