I've been thinking recently about how efficient my code is/isn't. Speed isn't a massive problem in any of my applications, by in a purely academic interest I have been wondering about the following:
I seem to have a excess of for-loops in my code. Is the speed of the execution drastically effected by using Matlab's in-built functions to create an entire matrix at once, rather than looping over each column?
Secondly, in terms of writing code more efficiently I have an example of some code I have written to fit a polynomial, of order n, to some data. Is there a better/quicker way of writing it?
function z = fitPoly(f,t,n)
f = f(:);
t = t(:);
X = nan(length(t), n+1);
for i = n:-1:0
X(:,n-i+1) = t.^(i);
end
z = X\f;
Thanks for your time
|
|
0
|
|
|
|
Reply
|
Jane
|
12/23/2009 9:47:02 PM |
|
"Jane " <j.l.terry@hotmail.co.uk> wrote in message <hgu34m$cs7$1@fred.mathworks.com>...
> I've been thinking recently about how efficient my code is/isn't. Speed isn't a massive problem in any of my applications, by in a purely academic interest I have been wondering about the following:
>
> I seem to have a excess of for-loops in my code. Is the speed of the execution drastically effected by using Matlab's in-built functions to create an entire matrix at once, rather than looping over each column?
>
> Secondly, in terms of writing code more efficiently I have an example of some code I have written to fit a polynomial, of order n, to some data. Is there a better/quicker way of writing it?
>
> function z = fitPoly(f,t,n)
>
> f = f(:);
> t = t(:);
>
> X = nan(length(t), n+1);
> for i = n:-1:0
> X(:,n-i+1) = t.^(i);
> end
>
> z = X\f;
>
> Thanks for your time
Is it always more efficient to avoid loops? This
depends on how big your loops are. But why
not do it right, and all in one simple line?
z = bsxfun(@power,t(:),n:-1:0)\f(:);
The use of bsxfun here completely avoids any
explicit loop, so it will be more efficient in
general. Try it out.
tic,z = bsxfun(@power,t(:),n:-1:0)\f(:);toc
Elapsed time is 0.002598 seconds.
tic
f = f(:);
t = t(:);
X = nan(length(t), n+1);
for i = n:-1:0
X(:,n-i+1) = t.^(i);
end
z = X\f;
toc
Elapsed time is 0.033657 seconds.
So the loop took 10 times as long. Neither one
is that slow, but why not get used to writing
efficient code? Stack up enough of these little
inefficiencies, and the difference will become
significant.
John
|
|
0
|
|
|
|
Reply
|
John
|
12/23/2009 10:08:04 PM
|
|
> Is it always more efficient to avoid loops? This
> depends on how big your loops are. But why
> not do it right, and all in one simple line?
>
> z = bsxfun(@power,t(:),n:-1:0)\f(:);
>
> The use of bsxfun here completely avoids any
> explicit loop, so it will be more efficient in
> general. Try it out.
>
> tic,z = bsxfun(@power,t(:),n:-1:0)\f(:);toc
> Elapsed time is 0.002598 seconds.
>
> tic
> f = f(:);
> t = t(:);
> X = nan(length(t), n+1);
> for i = n:-1:0
> X(:,n-i+1) = t.^(i);
> end
> z = X\f;
> toc
>
> Elapsed time is 0.033657 seconds.
>
> So the loop took 10 times as long. Neither one
> is that slow, but why not get used to writing
> efficient code? Stack up enough of these little
> inefficiencies, and the difference will become
> significant.
>
> John
Thanks, that was exactly the type of insightful tip I've been missing due to never having had to worry about speed or efficency.
|
|
0
|
|
|
|
Reply
|
Jane
|
12/23/2009 10:21:19 PM
|
|
Using this idea I have tried to change my function that takes the coefficent values of the polynomial and returns the values at t.
So from this
function f = formPoly(Z, t)
t = t(:);
order = length(Z) - 1;
f = zeros(length(t),1);
for i = 0:order
f(:) = f(:) + (Z(end-i)*(t.^i));
end
to this
function f = formPoly(Z, t)
order = length(Z) - 1;
f = sum(bsxfun(@times,bsxfun(@power,t(:),order:-1:0),Z(:)'),2);
Is that the correct interpretation of the idea?
|
|
0
|
|
|
|
Reply
|
Jane
|
12/23/2009 10:52:04 PM
|
|
|
3 Replies
197 Views
(page loaded in 0.184 seconds)
Similiar Articles: speed: Matlab vs C code - comp.soft-sys.matlabEfficiency of code - comp.soft-sys.matlab I've been thinking recently about how efficient my code is/isn't. Speed isn't a massive problem in any of my applications, by in ... Hardware decoding of adaptive dictionary based techniques - - comp ...Hi Folks, I'm doing some work where I store compressed program object code in ... I need an efficient decompression technique to avoid degrading the execution speed of ... efficiency in awk - comp.lang.awk(2) You might find that the program is 'fast enough' regardless of how you code it. If this is a one-time program, you probably don't need efficiency. How to create an efficient two dimensional VHDL arrays table ...Hi, Does anybody advise how to create a efficient vhdl 2 dimensional array table? For example for the below vhdl code:- library ieee; use ieee... Advantages of for and while loops? - comp.soft-sys.matlab ...... int(f(x),i-1,i) i=i+1; end syms x; for i=1:10 I=int(f(x),i-1,i) end Obviously the while loop is more code to write, but will it effect the efficiency ... Laptop specs for optimal matlab efficiency? - comp.soft-sys.matlab ...... automatically run in parallel, and reportedly some loops may have their code run ... Out of Memory in Matlab - comp.soft-sys.matlab Laptop specs for optimal matlab efficiency ... [FYI] MSXML HTTP translates response status code 204 to 1223 ...[FYI] MSXML HTTP translates response status code 204 to 1223 ..... had not been a priority there), and a Google Groups ... clear and efficient to perform a plain number ... MATLAB Vs C - comp.soft-sys.matlabI strongly suspect that you are more experienced with C than with MATLAB, which is why you were able to create more efficient C code than MATLAB code. Comparison of a Simple Join done by EG and Hand Coded - 22 times ...> > From what I have seen a SKILLED non-EG SQL programmer will =A0always > (99%of the time) create more efficient code than EG/SQL or handcoded > SQL?, albeit the code may ... Recursive Programming and Assembly Language - comp.lang.asm.x86 ...A recursive Quick Sort is a very standard use of recursion and if you can get the pivot code efficient enough, it does not need the small count additional sort like an ... Algorithmic efficiency - Wikipedia, the free encyclopediaFor example, a programmer might optimize code for time efficiency in an application for home computers (with sizable amounts of memory), but for code destined to be ... Florida Energy Efficiency Code for Building Construction | eHow.comFlorida's Energy Efficiency Code for Building Construction ensures that newly constructed homes in Florida perform to industry thermal and lighting standards to ... 7/16/2012 6:56:49 PM
|