开发者

Which is the best way to write Loops?

I would like to know Which is the best way to write Loops?

开发者_开发技巧

Is Count Down_to_Zero Loop better than Count_Up_Loops?And particularly in Embedded Systems context which one is better choice ???


In the embedded world it can be better to use one scheme in preference to another dependant upon the processor that you are using. For example the PIC processor has an instruction "decrement and jump if not zero". This is really good for doing a count down "for" loop in a single instruction.

Other processors have different instruction sets so different rules apply.

You may also have to factor in the effects of compiler optimisation which may convert a count up into the possibly more efficient count down version.

As always, if you have to worry about these things then you are using the wrong processor and tools. The idea of writing in a higher level language than assembler is to explain to the maintenance engineer how the software works. If it is counter intuitive to use a count down loop then don't, regardless of the (minor) loss in processor efficiency.


It's personal preference. In the case of arrays, counting up from 0 is usually better because you typically want to process each value in order. Neither style is inherently better, but they may have different results (e.g. if you were printing each value in an array, the order of the output would be different).

In many cases (with the notable exception of arrays), the most logical choice is to use a while loop rather than a for loop, e.g. reading from a file:

int c;
while ((c = fgetc(somefile)) != EOF)
    /* Do something */


The main thing to worry about is that you, or someone else, will read the code some time in the future, and person must be able to understand what the code is intended to do.

In other words, write your code in plain text. If you intend to do something ten times, loop from 0 to less-than-10. If you intend to walk backwards through an array, loop from the higher value to the lower.

Avoid placing anything that is not related to the control of the loop in the header of the for statement.

When it comes to efficiency, you can safely leave that to the compiler.


The best way to write a for loop is:

for(i=0; i<N; i++)
  for(j=0; j<N; j++)
  {
    array[i][j] = ... ;
  }

Everything else is "premature optimizations", ie things that the compiler really should be able to handle for you.

If you have a dumb compiler however, it may be more effective to count from N down to zero, as compare against zero is faster than compare against value on most CPUs.

Note that N should be a constant expression if possible. Leave out function calls like strlen() etc from the loop comparison.

++i will also be faster if the code might end up at a C++ compiler, where the C++ standard guarantees that ++i is faster than i++, because i++ creates a temporary invisible variable.

The order of the loop should be just as above for most systems, as this is often the most effective way to address cache memories, which is quite an advanced topic.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜