Optimizing a simple code
Show older comments
Hi People
I trying to optimize the following code and get ride of the for loop. What the code dose, is simple. It estimates the next column in a matrix based on the previous column values times a matrix.
clc
clear
close all
dt=0.01;
t=0:dt:10;
sls=zeros(2,numel(t));
M_c=[0 1;0 0];
M_c=M_c+[1 0;0 1]*1/dt;
V_s=[0;1];
for i=1:numel(t)-1
sls(:,i+1)=(M_c*sls(:,i)+V_s)*dt;
end
Any suggestion for a built-in function that would do the for loop part?
3 Comments
Geoff Hayes
on 20 Dec 2014
Why do you feel the need for optimization? Is the performance that poor as the code stands now?
John D'Errico
on 20 Dec 2014
After responding, I see it looks like Geoff and I think alike. Ergo, my condolences are due to Geoff. :)
AJ1
on 21 Dec 2014
Accepted Answer
More Answers (1)
John D'Errico
on 20 Dec 2014
People spend too much programmer time worrying about optimizing code that does not need it. In the end, they often get something that looks impressive, but uses as much or more time to run. Or you get something that is fine in theory, but in terms of numerical computer code does something potentially nasty.
A loop is fine there unless you are running the same code millions of times, and as long as you preallocate your sls matrix to the proper size, and you have done the latter.
On my machine, that entire code fragment took 0.0136 seconds to complete. I've spent something like a thousand times that just answering this question. If we added in the time spent by you posting it, and others reading it...
If you have something that works, USE IT. Only if it becomes a problem should you then look for bottlenecks to optimize. Remember that computer time is cheap. Programmer time is not. And, well, my time is free, worth every cent you paid for it. :)
set('soapBoxMode','down')
6 Comments
AJ1
on 20 Dec 2014
John D'Errico
on 20 Dec 2014
Edited: John D'Errico
on 20 Dec 2014
THEN YOU NEEDED TO SAY THAT!
How can we know that what you asked for is not really your problem? The mind reading toolbox is still under beta test, and has not been released to us.
The fact is, there are MANY methods in MATLAB to solve basic ODEs anyway, all far better than what you have written. So if your problem is how to solve a basic ODE in MATLAB, ASK for help about that.
You might start with
odeexamples
or
help funfun
wherein you will find many ODE solvers.
If your goal is simply to rant that MATLAB does not have a tool to explicitly solve a random problem (but you won't tell us your real problem) in a poor way, but to do it more efficiently, then I won't be of much help.
By the way, while it is possible that you MIGHT have been able to solve your problem using a tool like filter in some way, but that was not the problem you wanted to solve.
John D'Errico
on 21 Dec 2014
Edited: John D'Errico
on 21 Dec 2014
It is unfortunate that you want something that is not available to you. If you truly need speed for code to write a basic solver in a matrix form, then write it as C code. That is your option and always will be. The fact is, what you want is simply not that commonly asked for. It is just not that useful to enough people to make it something that will be provided by MathWorks. Yes, YOU think it is useful. As I said, then YOU can always write it.
Finally, there is no reason to insult people who tell you the truth. I have not attacked you personally, nor will I.
Roger Stafford
on 22 Dec 2014
AJ1, ordinarily I try to stay out of flame wars, but in this case I must speak out. I have read a great many of John D'Errico's comments and answers in both this MatlabCentral Answers Forum and the comp.soft-sys.matlab Usenet Newsgroup over a span of more than a decade, and I have also communicated with him directly a number of times. I know him to be one of the most astute contributors to these groups. Moreover he has written a number of very valuable File Exchange routines.
It is most inappropriate for you to be insulting him in the manner you have done here. You are very wrong to belittle his abilities in programming - he is an excellent programmer. You really do owe him an apology. In the absence of such an apology, I for one will not be contributing to any further queries you might make here in this forum.
Image Analyst
on 22 Dec 2014
Edited: Image Analyst
on 22 Dec 2014
AJ1's on my black list too. Even if he does apologize for saying "Ooos I wish people without solutions just stopped answering questions! Bro you simply don't know [profanity deleted] about programming!" to John, I won't answer. And of course there was at least one other prior comment with another name calling which has since been deleted.
Roger Stafford
on 22 Dec 2014
This was the original insult:
Dear John! When asking for help it is important to pose the most simple problem possible, it helps every one!. It is just unfortunate when an smart ass comes with advices rather than a solution!!!
That one needs an apology too.
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!