One Multi Dimensional Array or Several Two Dimensional Arrays ?

1 view (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 12 Jul 2015
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 Comments
Walter Roberson
Walter Roberson on 15 Jul 2015
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.
Mahmoud Sedahmed
Mahmoud Sedahmed on 18 Jul 2015
Thanks Walter, unless the form of multidimensional array is saving large run time i won't re-write the code, it would take a lot of time to re-write the code in the new form of multidimensional arrays, and i guess that won't help much. Although i would try vectorizing, Thanks a lot.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!