COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

Efficiency of code

• Email
• 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

See related articles to this posting

```"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
244 Views

Similar Articles

12/20/2013 4:22:41 PM
page loaded in 83599 ms. (0)

Similar Artilces:

code efficient
I have been starting to use Javascript a lot lately and I wanted to check with the "group" to get your thoughts on code efficiency. First, is there a good site/book that talks about good and bad ways to code. The reason I ask is because I was just thinking about the following...which is better and/or why? document.forms["myform"].elements["txtname"].value or document.myform.txtname.value Just looking for some insight. TIA -Bruce I believe the second looks quite efficient. Since the first one has to get the object from the array (searching for the object)....

efficient code
Would you please help me code the following algorithm in Matlab? Array has 3 rows with many columns.The elements are from zero to 1.I should add 1 if any of the array(1,:) element is less than -0.5 and I should subtract 1 if the array(1,:) element is more than +0.5. << But many of the elements of the first row of array(1,:) are less than 0.5 and greater than -0.5 >>. How can I code this efficiently? hemanth wrote: > > > Would you please help me code the following algorithm in Matlab? > > Array has 3 rows with many columns.The elements are > from zero to 1.I shou...

Efficient Code
I have large file in following format. Table A { 17.64.0.0|17; 32.58.34.192|26; 32.58.157.0|24; 32.106.15.0|24; } Table B { 32.106.80.0|21; 32.106.88.0|24; 32.106.192.0|20; } I want to read the file and store it in some data structure and then I will go through the data structure and according to some codition I will value I need and then I should also able to find it belongs which table (Table A or Table B). Which will efficient data structure to store this type data and then also retrive it fast to see in which table it exist. Right now I am using...

how to measure the Efficiency of code.
Hello I know about profile but is there any other tool available to measure whether the code is efficient. Clock? Tic? Uwe Brauer On 16 Feb, 12:11, Uwe Brauer <o...@mat.ucm.es> wrote: > Hello > > I know about profile but is there any other tool available > to measure whether the code is efficient. Clock? Tic? Define what you mean by 'efficient': - Run time? - Memory footprint? - Maintenance costs? - Time to development? Rune >>>>> On Tue, 16 Feb 2010 03:19:12 -0800 (PST), Rune Allnor <allnor@tele.ntnu.no> wrote: > On 16 Feb, 12:11, ...

Writing more efficient code
Greetings, and happyNewYear to all. I picked up Python a few weeks ago, and have been able to parse large files and process data pretty easily, but I believe my code isn't too efficient. I'm hoping dictionaries will help out, but I'm not sure the best way to implement them. I've been using a bunch of nested if/elif/else statements to select slices (0317 & 03de) from a file, then parse the data (aa, hh, bb, d2-d9) into parameters (a = airspeed, h = heading) & flags. #sample file contents 0000007d 03 0317 aa aa aa aa aa hh hh hh bb bb 0000007e 06 03de d2 d3 d4 d5 d6 d7...

Increasing code efficiency.
Hi all, I wrote some code a couple of weeks ago which I now think may have some runtime inefficiencies in it. I'm not sure if I've written it the right way but now I think I might be doing things in a potentially slow way, especially if I'm dealing with a monstrous layout of >40 cells. I'm thinking of using an array but somehow I'm stumped. Could anyone help me on this? Example of a typical input: ListIn = '( "nor2" "res4" "or6" "res7" "aoi3" "aoi1" ) Mode = "Hilite" *****************************...

Determining code efficiency
I am on the hunt for a tool (I am using Mac OS X), or a way, for determining javascript code efficiency. Something, except experience, that tells me for example which of the two code snippets below is the most efficient. Earlier I've been using the Firefox extension Firebug and its profiler tool, but you can't really make comparisons with it. // for-loop 1 for (var i=0; i < Things.length; i++) { Things[i] }; // for-loop 2 for (var i = Things.length - 1; i >= 0; i--){ Things[i] }; By the way, the second code snippet, for-loop 2, is what the Textmate Javascript Bundle ca...

Writing efficient code
I have heard that today two factors limit execution speed most often: The ability to keep everything in cache and branch predictions. I have at least some vague idea, how to help the execution environment to keep everything in the cache, i.e., when I write small code using a small amount of data and storage, it will be easier to keep it all within the cache. But is there anything I can do, when writing code, to help the processor to do an efficient (correct) branch prediction? "Stefan Ram" <ram@zedat.fu-berlin.de> wrote in message news:execution-20090108...

Help:efficient FSM coding
Hi everybody. I�d like to write an efficient fsm controller to control a AX88796 ethernet mac/phy from a Xilinx FPGA. The interface of the mac/phy is similar to a SRAM. The problem is that I have no idea, how I should make the code indipendent from the clock frequency, to respect all setup and hold constraints. How do I control that the state of the fsm changes only after a paramatrized number of clock cycles, without inferring counters and comparators (which wouldn't achieve a ~70MHz performance)? Best Regards Marco msd wrote: > The problem is that I have no idea, how I should > ...

Re: Efficient Code
Saggi, You are going in and out of DATA steps and PROCs way too much. You can also benefit by getting rid of unneeded variables and observations as soon as possible. Here's an example of some code revision. /* Code exported from SAS Enterprise Guide DATE: Wednesday, November 17, 2004 TIME: 04:12:04 PM PROJECT: Question for SAS-L PROJECT PATH: D:\R_A\Test\Question for SAS-L.seg */ /****************************************************************/ /* This is the data that needs to be fixed (discontinued data) */ %let OriginalData=main ; /* This is the data with all t...