Small Matrix (30x1) using significant time/memory to update in a loop

5 views (last 30 days)
I have a small vector Rv (30x1) which need to be updated in every loop. Before going in the loop, I have initiated the vector assigning values zeros. On profiling (with memory and time), I find that this updating step takes a significant amount of time compared to others. Also, the memory usage seems to be higher than for what a 30x1 matrix should use. I am attaching the image of profiler analysis. All the other variables appearing (like Kd K_Ir_Ac etc) are simple scaler variables.
What am I missing? What can I do the reduce the overhead in terms of memory and time usage?
This analysis was done for only a fraction of total time to test the complete function. On running the code upto final point (~100-200 times more loops that in this test run), these times will matter. And moreover, this is just out of curiosity as well!!
  1 Comment
dpb
dpb on 24 Dec 2013
Not enough info to tell for certain as there's no context to know what's invariant and what's not as well as sizes of everything else presuming some may not be simply constants?
Firstly, however, is there at least one redundant computation of the same values -- start by factoring the denominator out entirely from each computation and do it once at the end.
I didn't try to read the mishmash of similarly named variables to try to see what, if anything else, could either be factored or at least computed only once as a subfactor and used multiple locations.
But, need more context most of all methinks...

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 24 Dec 2013
Preallocating this vector with zeros is a waste of time! At least it is if immediately after that, you replace the entire vector with other numbers. So don't waste that time. (In general, preallocation is a VERY smart thing to do.)
Next, how many times must MATLAB compute the product (Nav*V), and then divide by it? Why force a computation to be done repeatedly? In fact, you go further than that, since you form things like Ir/(Nav*V) multiple times.
Next, I'd not be at all surprised to find that there is a difference in time between
Rv = zeros(30,1);
Rv(:,1) = [stuff];
and
Rv = [stuff];
Next, are ALL of these scalar variables changing all of the time? I would sincerely bet they are not. So why not precompute some parts of this vector and save the results?
Finally, I was going to try optimizing how your line is written, but since it is an image, I cannot copy it into the MATLAB editor.
  18 Comments
dpb
dpb on 29 Dec 2013
OK; just wanted to be sure you did get the benefit of the JIT engine included in the testing; otherwise one can draw entirely the wrong conclusion.
Those results are all consistent with what I would have expected.
Again, the only way I can see you can help much is by looking at the code that generates these values earlier on and see what, if anything, can be done there to vectorize that, again with the idea of eventually getting to the vectorized version of the end result. As noted previously, there may not be much else to be done in which case one can then start thinking about mex-ing stuff if there's a serious bottleneck and this will be done more than just once or twice so that can just let it take a couple of hours (or whatever) at some point and then be done with it.
Amit
Amit on 29 Dec 2013
when running the real simulation, the scalar value approah cumulatively takes more time than vector (probably cause the values are changing while in the test run, all the values were same). the bottleneck is not so significant that I'd try the mex approach .. my question was more out of curiosity. I learnt and understood matlab a bit more in this discussion.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!