One Multi Dimensional Array or Several Two Dimensional Arrays ?
2 次查看(过去 30 天)
显示 更早的评论
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 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!