One Multi Dimensional Array or Several Two Dimensional Arrays ?
6 ビュー (過去 30 日間)
古いコメントを表示
Mahmoud Sedahmed
2015 年 7 月 12 日
コメント済み: Mahmoud Sedahmed
2015 年 7 月 18 日
Hello everyone, I'm writing a code that includes calling back a specific variable several times, i used Matlab Profiler in order to determine the specific time for every part of the code and it was 95 % of the run time associated with calling back the variable values, Now the variable that's being called back is actually 9 variables or 9 arrays f1(i,j), f2(i,j) , f3 (i,j),... etc. where i,j are changing using a for loop in order to fill the arrays, So i have 9 arrays that are Two Dimensional,
My question is, Which is faster: 1) To run the code as it is using 9 Two Dimensional Arrays f1(i,j), f2(i,j), f3(i,j) ...etc. or 2) To use One Multi Dimensional Array f(i,j,1) , f(i,j,2) , f(i,j,3) ... etc.
Knowing that the functions f1,f2,f3.. etc. are not dependent on each other (can i run in parallel ?).
My Goal is to minimize the code run time.
Thanks in advance
Mahmoud
0 件のコメント
採用された回答
Walter Roberson
2015 年 7 月 12 日
Vectorize to do the entire function at one time. If you need to, use ndgrid:
[I, J] = ndgrid(1:maximum_i, 1:maximum_j);
f1(I,J)
3 件のコメント
Walter Roberson
2015 年 7 月 15 日
The time difference will be small for that situation. In my tests, the difference was about 1.5% at best, and varied from run to run; small differences in the code being executed would easily have overwhelmed the change.
Basically, unless you are expecting the code to be run for a few days of CPU, don't worry about it; it would take more of your time to work it out than to just execute as-is.
If you really really need to have it run faster, run timing tests on both versions using timeit(). But vectorizing could save you far more execution time than this ever would.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Numeric Types についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!