Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Which C code is better for the 2-loops?

Status
Not open for further replies.

ragabs

Full Member level 1
Joined
Jun 28, 2005
Messages
96
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
2,111
c question

hii to all

what is the better for the 2-loops

for(i=0;i<10;i++)
{//function doesnot depend on order}

and

for(i=10;i>0;i--)
{//function doesnot depend on order}


thanx for help
 

c question

It shouldn't matter. If your compiler has a good optimizer, then both forms will produce efficient code. If they don't, and if speed is important to you, then it's time to get a better compiler.
 

Re: c question

It does matter ;-)
and it depends on a number of things
First on the type of instructions your microcontroller has and on the number of cycles each instruction has.
Second on the fact whether your compiler uses the optimal instructions for your algorithm (better compiler can solve this)

Some controllers have efficient instructions to check if a variable is not equal to 0. The check if a variable is bigger then a constant might be less efficient (perhaps the constant should be fetched and put into a register every cycle). This check is done once every loop-cycle. Therefore decrementing a loop to zero might be more efficient.

But in your case I don't think there is a difference unless i is an unsigned char and your compiler converts the check "< 0" to "!= 0".
The most efficient solution will probably be

Code:
unsigned char i = 10;
for(; i!=0; i--)
{
  do_func();
}

BUT
think before you start optimizing. The difference in a loop with 10 iterations will probably be a 10 or 20 cycles. It depends on the number of cycles used in the do_func() routine to decide if it is worth to optimise there. Inlining functions, loop unfolding, loop merging and numerous other techniques might provide much better improvements ;-)

Antharax
 

c question

for 8051
for(i=10;i>0;i--)
{//function doesnot depend on order}

because 8051 have only DJNZ instruction.

it don't matter for PIC. because pic have DECFSZ and INCFSZ instructions.
 

c question

The purpose of an optimizer is to eliminate worries about how to write source code that produces efficient machine code. A good optimizer will rearrange loops, reverse loops, unroll loops, eliminate variables, etc.

If the original question had asked which loop format works better on a specific compiler, then it may have a specific answer. But not in general.
 

Re: c question

To summarise
There is no way of answering this questions in general!
Sometimes there will be a difference, sometimes there won't.

In an ideal world, all compilers whould make the most efficient code.
In practice it's not that easy, especially when you're short on RAM and CPU-cycles ;-)
- Does a good/better compiler exist for your platform
- Can you afford it for your platform
- ...
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top