Efficiency of code

  • Follow


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:













7/16/2012 6:56:49 PM


Reply: